javaScript reserved keywords
Asked Answered
H

1

8

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

Example:

According to:

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

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;
    }
};

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

Haley answered 23/7, 2011 at 9:39 Comment(0)
S
9

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']);
Survance answered 23/7, 2011 at 9:50 Comment(5)
So, to clarify, because the production rule for PropertyName uses IdentifierName and not Identifier, it's fine to use delete and so on as a PropertyName?Decorate
@Delan: I'd say yes. But that does not mean that browsers follow the same rule.Survance
Also if you run the code in "strict mode" it should throw an errorDelvecchio
Wow +1 for new thing for me :)Touslesmois
Accepted. Thanks for the updates to your answer as well, they provided some useful information : )Haley

© 2022 - 2024 — McMap. All rights reserved.