A, problem, put forward the
var o={x: 1};
delete o.x;//true
o.x;//undefined

but not variables to delete statement like this:

var x=1;
delete x;//false
x;//1

can't delete functions defined like this:

the function () {}
delete x x;//false
typeof x;//"function", "

note: can be deleted when the delete operator returns true said, return false said can't delete the

to understand this, we first need to acquire such as variable instantiation and attribute feature concept - it is a pity that these content in some javascript seldom talked about in the book. It's not difficult to understand them, if you don't care why they run, so you can skip this part.

2, code type var x=1;//Global code
the function test () {
var y=2;//Function Code
eval (" var z=3 ");//Eval Code in the Function

} Eval (" Function evalTest () {} ");//Eval Code in Global


3, the execution context
/* remember that ` this ` refers to the Global object when in Global scope */
var GLOBAL_OBJECT=this;

var foo=1;
GLOBAL_OBJECT. Foo;//1
foo.===GLOBAL_OBJECT foo;//true

the function bar () {}
typeof GLOBAL_OBJECT. Bar;//"function", "
GLOBAL_OBJECT. Bar===bar;//true

global variables into a global object properties, however, those defined in the Function code (Function code) of the local variable? Behavior is very similar: it became a variable object's properties. The only difference is that in the Function code (Function code), the variable object is not a global object, but the so-called active objects (Activation object). Each time the Function code (Function code) to perform the scope, will create an Activation object (Activation object).

not only Function code (Function code) of the attributes of the object variables and functions become activated, and the Function of each parameter (and corresponding parameter name) and a particular object of the Arguments. Note that activate the object is a kind of internal mechanisms, not be real access to the program code.

(function(foo){

var bar=2;
function baz(){}

/*
In abstract terms,

Special `arguments` object becomes a property of containing function's Activation object:
ACTIVATION_OBJECT.arguments;//Arguments object

...as well as argument `foo`:
ACTIVATION_OBJECT.foo;//1

...as well as variable `bar`:
ACTIVATION_OBJECT.bar;//2

...as well as function declared locally:
typeof ACTIVATION_OBJECT.baz;//"function"
*/

})(1);

in the end, the statement in the Eval code (Eval code) variables as the context calls the attributes of the object is created. Eval code (Eval code) using only which execution context it is called the variable object.

var GLOBAL_OBJECT=this;

/* ` foo ` is created as a property of calling the context Variable object,
which in this case is a Global object */

eval (' var foo=1);
GLOBAL_OBJECT. Foo;//1

(function () {

/* ` bar ` is created as a property of calling the context Variable object,
which in this case is an Activation object of containing the function */

eval (' var bar=1).

/* in abstract terms,
ACTIVATION_OBJECT. Bar;//1 */

}) ();

5, properties, characteristics,

var GLOBAL_OBJECT=this;

/* ` foo ` is a property of a Global object.
It is created via variable declaration and so has DontDelete attribute.
This is according to It can not be does. */

var foo=1;
delete foo.//false
typeof foo.//"number", "

/* ` bar ` is a property of a Global object.
It is created via the function declaration and so has DontDelete attribute.
This is according to It can not be does either. */

the function bar () {}
delete the bar;//false
typeof bar;//"function", "

/* ` baz ` is also a property of a Global object.
clear that it is created via the property the assignment and so has no DontDelete attribute.
This is according to the it can be does. */

GLOBAL_OBJECT. Baz='blah';
delete GLOBAL_OBJECT. Baz;//true
typeof GLOBAL_OBJECT. Baz;//"undefined"


6, built-in attributes and DontDelete

(function () {

/* can 't delete ` the arguments `, since it has DontDelete */

delete the arguments;//false
typeof the arguments;//"object"

/* can' t delete function 's ` length `; it also has DontDelete */

the function f () {}
delete f.l ength;//false
typeof f.l ength;//"number"

}) ();

function parameters and the corresponding attribute has DontDelete features created, therefore cannot be deleted.

(function (foo, bar) {

delete foo;//false
foo;//1

delete bar;//false
bar;//'blah'

}) (1, "blah");


7, did not declare the assignment of
var GLOBAL_OBJECT=this;

/* create global property via variable declaration; The property has DontDelete */
var foo=1;

/* create global property via undeclared the assignment; The property has no DontDelete */
bar=2;//can be understood as a window. The bar=2; According to the fifth above is can delete

delete foo.//false
typeof foo.//"number", "

delete the bar;//true
typeof bar;//"undefined"

please note that DontDelete features is set in the process of attributes, then the assignment will not modify the existing properties of existing features, it is very important to understand this.

/* ` foo ` is created as a property with DontDelete */
the function foo () {}

/* Later assignments do not modify the attributes. The DontDelete is still there! */
foo=1;
delete foo.//false
typeof foo.//"number", "

/* But assigning to a property that doesn 't exist,
creates that property with empty attributes (and so without DontDelete) */

this. Bar=1;
delete the bar;//true
typeof bar;//"undefined"

eight, Eval code

Eval (" var x=1 "); .
the console log (x);//1
delete x; .
the console log (typeof x);//undefined

eval (" function test () {var x=1; the console log (delete) x;/* */false; return 1;} ");
the console. The log (test ());//1
delete the test;
the console. The log (typeof test);//undefined

note that here in the Eval created a variable or method does not include the internal variables or methods, such as the red part of the code above, still tell before is the same: cannot be deleted.

9, FireBug confusion

var x=1;
delete x; .
the console log (typeof x);//undefined

the function y () {
var z=1; .
the console log (delete z);//false

} y ();
delete y; .
the console log (typeof y);//undefined

it's clearly a breach of the above rules, but found that after the first eight contrast with the above, this is the effect of code execution in the eval. Although not confirmed, but I guess the FireBug (Chrome Developer tool) code is to use the console in the eval.

so, when you test the JS code, if it comes to the current context environment pay close attention to.

10, delete operator
delete the Object class Object {
public:
Object * x;
}

Object o;
o.x=new Object ();
delete o.x;//Object with a new Object will be released on the

but Javascript delete, unlike c + +, it does not delete o.x, pointing to the Object, but to remove o.x attribute itself.

var o={};
o.x=new Object ();
delete o.x;//on a line of the new Object remains
o.x;//undefined, o attribute is called x deleted

in the actual Javascript, delete o.x, the Object will be garbage collection due to lost reference, so the delete o.x also "equivalent to" delete o.x is pointing to the Object, but this action is not the ECMAScript standard, that is to say, even if an implementation does not delete the Object, also not be in violation of the ECMAScript standard.

"delete properties not delete the object" that can be identified by the following code.

var o={};
var a={x: 10};
o.a=a;
delete o.a.//o.a attribute is deleted
o.a.//undefined
a.x;//10, because 10} {x: object is a reference, so will not be recycled

in addition, the delete o.x can also write the delete o (" x "), the effect is the same.

11, other attributes cannot be deleted

the function () {C this. X=42; }
C.p rototype. X=12; .
C.p rototype y=13;

var o=new C ();
o.x;//42, constructor defined o.x

delete o.x;//is true delete itself define x
o.x;//12, the prototype is defined in o.x, even if again delete o.x will not be deleted

delete o.y;//true, because no o.y o their own property, y exists in the prototype chain, that is the object itself is different and prototype attribute
o.y;//13


summary

the above said so much, hope to help everyone know JavaScript the Delete. Due to the limited level, does not guarantee that absolutely right, if found wrong please correct me.

to:
1, http://perfectionkills.com/understanding-delete/(English)
2, http://nanto.asablo.jp/blog/2008/01/09/2552470 (Japanese)

in this paper, starting at http://jscode.cnblogs.com

you might be interested in the article:

  • Javascript study notes delete operator
  • Javascript delete using the sample code
  • Javascript delete the use of the attribute
  • Javascript delete operator applications
  • delete use of Javascript,


This concludes the body part