Reproduced from: http://tech.idv2.com/2008/01/09/javascript-variables-and-delete-operator/
Just saw a good article (the original link), the JavaScript in the delete operator is very thorough analysis. Here is a brief introduction to the content.
Although it is a small delete operator, its behavior is extremely complex. JavaScript variable Delete operator delete object on variable execute delete can delete properties and properties that cannot be deleted the variables that can be deleted and the variables that cannot be deleted the return value JavaScript variables
In fact, in JavaScript, variables = Object properties, because JavaScript creates a global object before executing the script, all global variables are properties of the global object, and a activation object is created when the function is executed. All local variables are attributes of this activation object. The following example:
var global =;
This.global; 42, you can use this to access the global object
this.global2 =;
Global2; The
function foo () {
var local =;
However, you cannot access the activation directly,
//Therefore you cannot access the local variable in a foo.local manner
}
object deleted by the delete operator
C + + also has the delete operator, which deletes the object that the pointer points to. For example:
C + +
class object {public
:
object *x;
}
Object o;
o.x = new Object ();
Delete o.x; Object on line new will be freed
But the JavaScript delete is different from C + +,
It does not delete the object that o.x points to, but instead deletes the O.x property itself。
Javascript
var o = {};
o.x = new Object ();
Delete o.x; The object object of the new line is still
o.x; Undefined,o's attribute named X is deleted.
In actual JavaScript, after delete o.x, object objects are garbage collected because they have lost references, so the delete o.x is "equivalent" The object pointed to by o.x is deleted, but this action is not a ECMAScript standard, that is, even if an implementation does not delete the object at all, it is not a violation of the ECMAScript standard.
"Delete attributes instead of deleting objects" can be confirmed by the following code.
var o = {};
var a = {x:10};
O.A = A;
Delete o.a; The O.a property is deleted
o.a; Undefined
a.x; 10 because the {X:10} object is still referenced by a, so it will not be reclaimed
In addition, the delete o.x can write the delete o["X", both of which have the same effect. performing a delete on a variable
Because the variable is also Global or a property of the activation object, the delete operation on the variable is the same result.
var global =;
Delete global; Delete Global.global
function foo () {
var local =;
Delete local; Delete Activation.local
}
Properties that can be deleted and properties that cannot be deleted
Not all attributes can be delete. For example, a property declared in prototype cannot be delete:
function C () {this.x = n;}
c.prototype.x =;
var o = new C ();
o.x; 42, the o.x delete o.x defined in the constructor
;
o.x; The o.x defined in prototype is not deleted even if the delete o.x is executed again
The predefined properties of the object cannot be deleted either. This type of attribute can be considered to have dontdelete characteristics.
var re =/abc/i;
Delete re.ignorecase;
Re.ignorecase; True, ignorecase cannot be deleted
variables that can be deleted and variables that cannot be deleted
Variables declared through Var and functions declared by function have the Dontdelete attribute and cannot be deleted.
var x =;
Delete x;
x; X is not deleted
y = A;
Delete y;
y; Undefined
function foo () {return;}
Delete foo;
Foo (); 42
One exception, however, is that in code executed through eval, variables declared through Var are the same as the global object with the normal var declaration variables, but they do not have the Dontdelete attribute and can be deleted.
Eval ("var x =;");
x;
delete x;
x; Undefined
But there is one exception to this, where the variables defined by VAR in the Eval code are dontdelete and cannot be deleted
Eval ("(function () {var x = 42; Delete x; return x; })();");
Return 42
The return value of the delete
Delete is a normal operator and returns TRUE or false. Rule is: Returns False when the property of the object being removed is present and has dontdelete, otherwise returns true. One feature here is that when an object property does not exist, it returns true, so the return value is not exactly the same as whether the deletion was successful or not.
function C () {this.x = n;}
C.prototype.y =;
var o = new C ();
Delete o.x; True
o.x; Undefined
"x" in O; False
//o.x exists and does not have Dontdelete, returns true
delete o.y;//True
o.y; A
/O itself has no o.y property, so return True
//From here you can see the existence of the prototype chain, the object itself and the prototype property are different
delete o; False
//GLOBAL.O owns Dontdelete attribute so returns false
delete Undefinedproperty; True
//global does not have a property named Undefinedproperty and therefore returns true
delete; True
//42 is not an attribute so returns TRUE. Some implementations throw an exception (violating the ECMAScript standard)
var x =;
Delete x + +; True
x;
//deleted is the return value of X + + (24), not the property, so return True