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().
There are a total of 63 reserved words in JavaScript.
Rules for Naming JavaScript IdentifiersKeywords cannot be used as identifier names.
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.
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 anIdentifier
.
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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With