最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How do I use a Javascript keyword as a variable name? - Stack Overflow

programmeradmin4浏览0评论

If I want to use the following in JS, Is some option available to do it?

var break = "";

In C# we have the option to use it using @, is similar option available in JS

public int @public;

If I want to use the following in JS, Is some option available to do it?

var break = "";

In C# we have the option to use it using @, is similar option available in JS

public int @public;
Share Improve this question asked Jul 28, 2016 at 20:15 VijaiVijai 2,5273 gold badges28 silver badges34 bronze badges 2
  • 4 You don't, for obvious reasons. And no, there's no way to use reserved keywords as variable names, and why would you, you have an infinite number of other choices than the few reserved words. – adeneo Commented Jul 28, 2016 at 20:16
  • 1 Just use another character in front of it, like a $ or _ – castletheperson Commented Jul 28, 2016 at 20:20
Add a ment  | 

2 Answers 2

Reset to default 8

Well, if you insist, you can

var $break = 1;

which is the same as C# in terms of characters ;)

Seriously, you cannot use reserved keywords as variables, but they are allowed as property names:

myObj = {}
myObj.break = 'whatever';

Also, do note that the repertoire of the reserved words varies depending on the strictness. For example,

var interface = 1;

is valid in the non-strict mode, but breaks once you add use strict to your script.

You can't use Javascript keywords as a variable name.

发布评论

评论列表(0)

  1. 暂无评论