5

According to EcmaScript specification some objects properties cannot be deleted due to the DontDelete internal parameter. For example :

var y = 5

should 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 ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
elreyano
  • 95
  • 3

3 Answers3

2

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.

Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • from what I was able to understand it allows to delete global variables declared without 'var' exactly because they're global objects properties. – elreyano May 13 '11 at 07:16
1

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

  1. 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...

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Please note that `SetMutableBinding` does not set the "delete" flag. Your second quote seems to be unrelated to the first. – Guss Sep 29 '14 at 08:58
  • This is a bit old, but maybe it needs to be updated. The first relates to how declared functions and variables are created by *CreateMutableBinding*, the second part to how it's called, and yes, it should reference *CreateMutableBinding*. I'll update this soon. – RobG Sep 29 '14 at 13:17
0
var y = 5
alert(delete (y));

Show false. Then, can't be deleted.

ariel
  • 15,620
  • 12
  • 61
  • 73