Today when I was looking at the prototype code, I discovered the delete operator.
unset: function(key) {
var value = this._object[key];
delete this._object[key];
return value;
}
I checked the manual,
The delete operator deletes a property from an object or an element from an array.
delete expression
The expression parameter is a valid JScript expression, usually a property name or array element.
Description If the result of expression is an object and the attribute specified in expression exists, and the object does not allow it to be deleted, false is returned.
In all other cases, returns true.
It feels good to see "delete an element from the array", but after trying it in ff, it seems that it can only delete the value of that element, not the element itself. However, it is possible to delete a property from an object.
I googled again and found an article that goes into great detail, so I’ll repost it here so I don’t forget:
Title: Javascript Variables and Delete Operator Copyright Statement: You can reprint at will, but you must indicate the original author charlee and the original link http://tech.idv2.com/2008/01/09/javascript-variables-and-delete when reprinting -operator/ and this statement.
content:
I just read a good article (original link), which provides a thorough analysis of the delete operator in Javascript. Here is a brief introduction to the content.
Although it is a small delete operator, its behavior is extremely complex.
Javascript variables
Object deleted by delete operator When delete is executed on a variable, attributes that can be deleted and attributes that cannot be deleted, variables that can be deleted, and variables that cannot be deleted
Return value of delete
------------------------------------------------- ----------------------------------
Javascript variables In fact, in Javascript, variables = object properties, this is because Javascript will create a Global object before executing the script. All global variables are properties of this Global object. When executing the function, an Activation object will also be created. All local variables are properties of this Activation object. For example:
var global = 42;this.global; // 42, you can access the Global object through this this.global2 = 12;global2; // 12function foo() { var local = 36; // However, Activation cannot be accessed directly , // Therefore, the local variable cannot be accessed through foo.local} The object deleted by the delete operator
There is also a delete operator in C++, which deletes the object pointed to by the pointer. For example:
// C++class Object {public: Object *x;}Object o;ox = new Object();delete ox; // The new Object object in the previous line will be released, but Javascript's delete is different from C++. The object pointed to by ox is not deleted, but the ox attribute itself is deleted.
// Javascriptvar o = {};ox = new Object();delete ox; // The Object object new in the previous line still exists ox; // undefined, the property named x of o is deleted. In the actual Javascript, After deleting ox, the Object object will be garbage collected because it has lost its reference. Therefore, deleting ox is "equivalent to" deleting the object pointed to by ox. However, this action is not an ECMAScript standard. That is to say, even if an implementation is completely Not deleting the Object object does not violate the ECMAScript standard.
"Deleting attributes instead of deleting objects" can be confirmed by the following code.
var o = {};var a = { x: 10 };oa = a;delete oa; // The oa attribute is deleted oa; // undefineda.x; // 10, because the { x: 10 } object is still a Reference, so it will not be recycled. In addition, delete ox can also be written as delete o["x"], both of which have the same effect.
When deleting a variable, the variable is also a property of the Global or Activation object, so the delete operation on the variable has the same result.
var global = 42; delete global; // Delete Global.globalfunction foo() { var local = 36; delete local; // Delete Activation.local} Properties that can be deleted and properties that cannot be deleted Not all properties can be deleted delete. For example, properties declared in prototype cannot be deleted:
function C() { this.x = 42; }C.prototype.x = 12;var o = new C();ox; // 42, defined in the constructor o.xdelete ox;ox; // 12, ox defined in the prototype will not be deleted even if delete ox is executed again. The predefined properties of the object cannot be deleted. This type of attribute can be considered to have the characteristics of DontDelete.
var re = /abc/i;delete re.ignoreCase;re.ignoreCase; // true, ignoreCase cannot delete variables that can be deleted and variables that cannot be deleted. Variables declared through var and functions declared through function have the DontDelete attribute and cannot be deleted. delete.
var x = 36;delete x;x; // 36, x is not deleted y = 12;delete y;y; // undefinedfunction foo() { return 42; }delete foo;foo(); // 42 but there is One exception is that in code executed through eval, although variables declared through var belong to the same Global object as normal var-declared variables, they do not have the DontDelete attribute and can be deleted.
eval("var x = 36;");x; // 42delete x;x; // undefined But there is an exception. Variables defined by var in the function in the eval code have DontDelete and cannot be deleted eval(" (function() { var x = 42; delete x; return x; })();"); // Return the return value of 42delete
Delete is a normal operator and returns true or false. The rule is: when the property of the deleted object exists and has DontDelete, it returns false, otherwise it returns true. One feature here is that true is returned even when the object attribute does not exist, so the return value is not completely equivalent to whether the deletion is successful or not.
function C() { this.x = 42; }C.prototype.y = 12;var o = new C();delete ox; // trueo.x; // undefined"x" in o; // false/ / ox exists and there is no DontDelete, return truedelete oy; // trueo.y; // 12// o itself has no oy attribute, so return true// From here you can also see the existence of the prototype chain, the object's own attributes and prototype attributes is different delete o; // false// Global.o has the DontDelete attribute so it returns falsedelete undefinedProperty; // true// Global has no property named undefinedProperty so it returns truedelete 42; // true// 42 is not a property so it returns true . Some implementations will throw an exception (violating the ECMAScript standard) var x = 24; delete x++; // truex; // 25// What is deleted is the return value (24) of x++, not an attribute, so true is returned