The delete operator deletes an attribute from an object or an element from an array. Today, when reading prototype code, we found the delete operator.
The Code is as follows:
Unset: function (key ){
Var value = this. _ object [key];
Delete this. _ object [key];
Return value;
}
Check the manual,
Delete Operator
Deletes an attribute from an object or an element from an array.
Delete expression
The expression parameter is a valid JScript expression, usually an attribute name or an array element.
Description
If expression results in an object and the attribute specified in expression exists, and the object cannot be deleted, false is returned.
Returns true in all other cases.
It looks good to see "deleting an element from an array", but I tried it in ff. It seems that only the value of that element can be deleted, not the element itself. However, you can delete an attribute from an object.
I Googled another article and found it detailed. I reprinted it to avoid forgetting:
Javascript Variables
In Javascript, the variable = Object Property is because Javascript creates a Global object before executing the script, and all Global variables are attributes of this Global object, when the function is executed, an Activation object is created. All local variables are attributes of this Activation object. For example:
The Code is as follows:
Var global = 42;
This. global; // 42. You can use this to access Global objects.
This. global2 = 12;
Global2; // 12
Function foo (){
Var local = 36;
// However, you cannot directly access Activation,
// Therefore, the local variable cannot be accessed through foo. local.
}
Objects deleted by the delete Operator
C ++ also has the delete operator, which deletes the object pointed to by the pointer. For example:
The Code is as follows:
// C ++
Class Object {
Public:
Object * x;
}
Object o;
O. x = new Object ();
Delete o. x; // The new Object in the previous row will be released.
However, unlike C ++, Javascript delete does not delete the objects pointed to by o. x, but deletes the o. x attribute itself.
The Code is as follows:
// Javascript
Var o = {};
O. x = new Object ();
Delete o. x; // The new Object in the previous Row still exists.
O. x; // undefined. The attribute named x of o is deleted.
In actual Javascript, delete o. after x, the Object will be reclaimed due to the loss of reference, So delete o. x is equivalent to deleting o. x points to the Object, but this action is not the ECMAScript standard. That is to say, even if an implementation does not delete the Object at all, it is not in violation of the ECMAScript standard.
You can use the following code to confirm "delete attributes rather than objects.
The Code is as follows:
Var o = {};
Var a = {x: 10 };
O. a =;
Delete o. a; // The o. a attribute is deleted.
O. a; // undefined
A. x; // 10, because the {x: 10} object is still referenced by a, it will not be recycled.
In addition, delete o. x can also write delete o ["x"], with the same effect.
Executing delete on variables
Because the variable is also a Global or Activation object attribute, the delete operation on the variable is the same result.
The Code is as follows:
Var global = 42;
Delete global; // delete Global. global
Function foo (){
Var local = 36;
Delete local; // delete Activation. local
}
Attributes that can be deleted and attributes that cannot be deleted
Not all attributes can be deleted. For example, the attribute declared in prototype cannot be deleted:
The Code is as follows:
Function C () {this. x = 42 ;}
C. prototype. x = 12;
Var o = new C ();
O. x; // 42, the o. x defined in the constructor
Delete o. x;
O. x; // 12. o. x defined in prototype will not be deleted even if delete o. x is executed again.
The predefined attributes of an object cannot be deleted. This type of attribute is considered to have the DontDelete feature.
The Code is as follows:
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 through function have the DontDelete feature and cannot be deleted.
The Code is as follows:
Var x = 36;
Delete x;
X; // 36, x not deleted
Y = 12;
Delete y;
Y; // undefined
Function foo () {return 42 ;}
Delete foo;
Foo (); // 42
One exception is that the variables declared through var in the Code executed through eval belong to the Global object, but they do not have the DontDelete feature and can be deleted.
The Code is as follows:
Eval ("var x = 36 ;");
X; // 42
Delete x;
X; // undefined
But this is also an exception. The variables defined by var in the eval Code have DontDelete and cannot be deleted.
The Code is as follows:
Eval ("(function () {var x = 42; delete x; return x ;})();");
// Return 42
Return Value of delete
Delete is a common operator and returns true or false. The rule is: if the property of the object to be deleted exists and has DontDelete, false is returned; otherwise, true is returned. One feature here is that true is returned if the object property does not exist, so the return value is not exactly the same as whether the deletion is successful or not.
The Code is as follows:
Function C () {this. x = 42 ;}
C. prototype. y = 12;
Var o = new C ();
Delete o. x; // true
O. x; // undefined
"X" in o; // false
// If o. x exists and no DontDelete exists, true is returned.
Delete o. y; // true
O. y; // 12
// O itself does not have the o. y attribute, so true is returned.
// You can also see the existence of the prototype chain from here. The attributes of the object and prototype are different.
Delete o; // false
// Global. o has the DontDelete feature, so false is returned.
Delete undefinedProperty; // true
// Global does not have an attribute named undefinedProperty. Therefore, true is returned.
Delete 42; // true
// 42 is not an attribute, so true is returned. Some implementations throw an exception (in violation of the ECMAScript Standard)
Var x = 24;
Delete x ++; // true
X; // 25
// The deleted value is the return value of x ++ (24), which is not an attribute. Therefore, true is returned.