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().
PropertyName
usesIdentifierName
and notIdentifier
, it's fine to usedelete
and so on as aPropertyName
? – Decorate