Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 418
Willem Avatar asked Jul 23 '11 09:07

Willem


People also ask

How many reserved keywords are there in JavaScript?

There are a total of 63 reserved words in JavaScript.

Can we use reserved keywords as identifiers in JavaScript?

Rules for Naming JavaScript IdentifiersKeywords cannot be used as identifier names.

What is difference between keywords and reserved words?

Keywords have a special meaning in a language, and are part of the syntax. Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language. In practice most keywords are reserved words and vice versa.


1 Answers

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']);
like image 107
Felix Kling Avatar answered Sep 28 '22 11:09

Felix Kling