DontDelete property of javascript objects
According to EcmaScript specification some objects properties cannot be deleted due to the DontDelete internal parameter. For example :
var y = 5
shou开发者_C百科ld not be deletable. But from what I was able to check - it is.
Here's a link at Mozilla Developer Center : https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/delete
Any ideas why this isn't working as it should ?
Sometimes you have to check what you read. There is no DontDelete
internal parameter in the ECMA-specification (262, ed 5). Maybe the [Configurable
] property is meant? The delete
operator doesn't work on variables or functions, it works on object properties:
var y=5,
z = {y:5};
delete y;
delete z.y;
alert(y); //=> 5
alert(z.y); //=> undefined
From my answer, this SO question emerged, and an excellent answer from T.J. Crowder.
According to ES5 table 17:
CreateMutableBinding(N, D) Create a new mutable binding in an environment record. The String value N is the text of the bound name. If the optional Boolean argument D is true the binding is may be subsequently deleted.
and in 10.5 Declaration Binding Instantiation
- For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do [...] ii. Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.
Which says to me that declared variables should be not deleteable. In global code, the global object is the activation object which is the variable obejct, so declared globals shouldn't be deletable. Of course, browsers may not adhere to that...
var y = 5
alert(delete (y));
Show false. Then, can't be deleted.
精彩评论