javaScript保留关键字 [英] javaScript reserved keywords
问题描述
我想知道如何管理JavaScript的保留关键字/功能。
I am wondering how JavaScript's reserved keywords / functions are managed.
示例:
根据:
http://www.quackit.com/javascript/javascript_reserved_words.cfm
delete
是JavaScript提供的保留关键字。
delete
is a reserved keyword by JavaScript.
然后考虑以下代码片段:
Then consider the following snippet for some context:
var cookieManager = {
get: function (name) {
// function contents ...
console.log("cookieManager.get() called");
return true;
},
set: function (name, value, days) {
// function contents ...
console.log("cookieManager.set() called");
return true;
},
delete: function (name) {
// function contents ...
console.log("cookieManager.delete() called");
return true;
}
};
此对象有一个 delete
属性它的名称是由JavaScript保留的,所以它应该失败,对吧?
This object has a delete
property, yet the name of it is reserved by JavaScript so it should fail, right?
然而当我执行时,cookieManager.delete();
在 webconsole
的 FireFox
我得到以下输出,表明它工作正常:
Yet when I execute cookieManager.delete();
in the webconsole
of FireFox
I get the following output, suggesting that it works fine:
[11:26:00.654] cookieManager.delete();
[11:26:00.656] cookieManager.delete() called
[11:26:00.657] true
但是,如果您在 JsLint
中运行代码,则表示
If, however, you run the code in JsLint
it says
Problem at line 12 character 5: Expected an identifier and instead saw 'delete' (a reserved word).
delete: function (name) {
建议这是一个很大的没有没有方法,应该避免。
Suggesting that this is a big no no approach and should be avoided.
所以我什么时候应该考虑保留关键字,就像在这个例子中它似乎工作就像我希望它(delete关键字在对象cookieManager的上下文中,因此不会导致冲突,因此可以使用)或者我应该遵守 JsLint
并通过javascript重命名任何保留关键字?在这种情况下,我可以轻松地将.delete()重命名为.remove()。
So when should I take reserved keywords into consideration, as in this example it seems to work just like I want it to ( the delete keyword is in the context of the object cookieManager and thus causes no conflicts, hence it can be used ) or should I abide to the bible that is JsLint
and rename anything that is a reserved keyword by javascript? In this context I could easily rename .delete() to .remove().
推荐答案
实际上这是允许的,因为 ECMAScript规范。对象文字的制作规则(第11.1.5节)是:
Actually this is allowed as per ECMAScript specification. The production rules (Section 11.1.5) for an object literal are:
ObjectLiteral :
{}
{PropertyNameAndValueList}
{PropertyNameAndValueList ,}
PropertyNameAndValueList :
PropertyAssignment
PropertyNameAndValueList , PropertyAssignment
PropertyAssignment :
PropertyName : AssignmentExpression
get PropertyName ( ){FunctionBody}
set PropertyName (PropertySetParameterList){FunctionBody}
PropertyName :
IdentifierName
StringLiteral
NumericLiteral
在您的情况下,您使用 IdentifierName
作为属性名称。 第7.6.1节说:
In your case, you use an IdentifierName
as property name. Section 7.6.1 says:
保留字是
IdentifierName
,不能用作标识符
。
注意区别:您不能将保留关键字用作标识符,但因为它是有效的 IdentifierName 您可以将其用作 PropertyName 。
Note the difference: You cannot use a reserved keyword as Identifier, but as it is a valid IdentifierName you can use it as PropertyName.
尽管如此,其他(版本)浏览器可能不会容忍这一点,所以为了安全起见,我会重命名它。
Nevertheless, other (versions of) browsers might not tolerate this, so to be on the safe side, I would rename it.
除了浏览器可能出现的问题外,它还可能会混淆其他人阅读您的代码并且不熟悉此规则。
Apart from possible problems with browsers, it might also confuse others who read your code and are not familiar with this rule.
FWIW,当然您总是可以使用保留关键字作为字符串:
FWIW, of course you can always use reserved keywords as strings:
var a = {'delete': 'foo'};
alert(a['delete']);
这篇关于javaScript保留关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!