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

时间:2016-07-28 20:15:53

标签: javascript c# jquery

如果我想在JS中使用以下内容,是否可以使用某些选项?

var break = "";

在C#中,我们可以选择使用@,它是JS中可用的类似选项

public int @public;

2 个答案:

答案 0 :(得分:6)

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.

答案 1 :(得分:4)

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