2

If I do this in JSDB 1.8 which uses Spidermonkey 1.8:

 this.x = 3;
 var y = 4;
 function z() { return 77; }
 this.w = function w() { return 44; }
 this.v = function v() { return 55; }
 w = function w() { return 66; }
 function v() { return 77; }
 delete x;
 delete y;
 delete z;
 delete w;
 delete v;

I get true from the delete x and delete w lines, but false from the delete y and delete z and delete v lines.

What's going on here, and is this behavior defined in the ECMAscript standard or in Spidermonkey? I wanted to remove a function from a particular scope and found that I could not.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 3
    i'm to lazy to type out an example, read the entire in detail description of delete here : http://perfectionkills.com/understanding-delete/ – Willem D'Haeseleer Apr 02 '12 at 17:12
  • @helmus that should be an answer:) – tkone Apr 02 '12 at 17:20
  • 1
    I know, but I dont want to see the dreadful "trivial answer transformed to comment" message because im only posting a link. So im keeping my pride by making it a comment in the first place ;) – Willem D'Haeseleer Apr 02 '12 at 17:26

3 Answers3

1

read the entire in detail description of delete here http://perfectionkills.com/understanding-delete/

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99
1

Another good link of describing this behavior is from MDN https://developer.mozilla.org/en/JavaScript/Reference/Operators/delete

which states that variables defined using var keyword in global namespace can't be deleted.

but if you are using javascript 1.8.5 then you can make use of defineProperty method of Object to create properties which can be deleted by settings configurable option to true

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty

Saket Patel
  • 6,573
  • 1
  • 27
  • 36
0

As the link Helmus posted explains, broadly speaking (there are some cross-browser quirks in this field, as the article discusses), variables cannot be deleted but properties can.

Note that, in the global scope, variables declared without the var keyword are considered properties of the global object. So:

var global_var1 = 'some val';
global_var2 = 'some val';
delete global_var1; //false
delete global_var2; //true
typeof global_var1; //'string'
typeof global_var2; //'undefined'