Javascript delete operator

I read a javascript book that said:

var o = {x:1 , y:2 };
delete o ; // Can't delete a declared variable so returns false;

However, the book also states that variables declared outside of any function region are properties of a global object.

Why can't we remove it if this is a property of a global object?

+4
source share
3 answers

Saying:

var o = {x:1 , y:2 };

in the top-level area, you declare a global variable that cannot be deleted. It creates a property on the global object (which is flattened by the object windowin browsers), but this is a really special property. However, if you make the expression as follows:

o = {x:1 , y:2 };

(, window) . , . delete , , .

,

fooobar.com/questions/19349/...

+3

O , delete .

var o = {x:1 , y:2 };
delete o.x ; 

DEMO .

+1

, delete operator.let me explain ---

"The delete operator removes a property from an object".here i say it removes an object property not a variable.

in your code you declare a variable.not an object so delete does not work.i think you understand.

o = {x:1 , y:2 }; delete o ;

, . ? o , , . , ,

. .

-1
source

All Articles