开发者

javaScript reserved keywords

I am wondering how JavaScript's reserved keywords / functions are managed.

Example:

According to:

http://www.quackit.com/javascript/javascript_reserved_words.cfm

delete 开发者_Python百科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;
    }
};

This object has a delete property, yet the name of it is reserved by JavaScript so it should fail, right?

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

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.

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().


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

In your case, you use an IdentifierName as property name. Section 7.6.1 says:

A reserved word is an IdentifierName that cannot be used as an Identifier.

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, of course you can always use reserved keywords as strings:

var a = {'delete': 'foo'};
alert(a['delete']);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜