简体   繁体   English

如何使用Javascript关键字作为变量名?

[英]How do I use a Javascript keyword as a variable name?

If I want to use the following in JS, Is some option available to do it? 如果要在JS中使用以下内容,是否可以使用某些选项?

var break = "";

In C# we have the option to use it using @, is similar option available in JS 在C#中,我们可以选择使用@来使用它,这与JS中可用的类似选项

public int @public;

Well, if you insist, you can 好吧,如果你坚持,你可以

var $break = 1;

which is the same as C# in terms of characters ;) 就字符而言,这与C#相同;)

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. 在非严格模式下有效,但是一旦在脚本中添加use strict就会中断。

您不能将Javascript关键字用作变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM