Javascript Delete example
CopyCode Code: var flower = {};
Flower. Name = "Oop ";
Delete flower. Name; // true
Alert (flower. Name); // undefined
Create an object named flower
Flower has the member name and value "Oop ";
Delete operation to delete this Member
Deleted successfully. The member flower. name does not exist.
Javascript Delete Example 2Copy codeThe Code is as follows: Alert (isnan (5); // false
Delete isnan; // true
Alert (isnan (5); // undefined
The delete operator can even delete members of the global object.
Variables declared by VAR cannot be deleted.Copy codeThe Code is as follows: var flower = "monkey ";
Delete flower; // false
Alert (flower); // "monkey"
If the variable stated with VAR is deleted, false is returned after the delete operation. The variable does not exist successfully;
NOTE: If delete is used to delete a member that cannot be deleted, false is returned.
The variables of the Host object cannot be deleted in IE.Copy codeThe Code is as follows: window. Flower = "monkey ";
Delete flower; // throw an exception
Alert (flower );
In the FF BrowserCopy codeThe Code is as follows: window. Flower = "monkey ";
Delete flower; // true
Alert (flower) // undefined
When we can see the members of The delete window, the browser performance is inconsistent.
Window is the Javascript Host object.
The Host object can be defined by the Javascript execution environment.
In the ie6-8 browser, You cannot delete window. Flower, the browser will prompt you "object does not support this operation", that is, cannot delete members under the window
You cannot delete a function declared by function name.Copy codeCode: function flower (){}
Delete flower; // true
Alert (flower); // undefined
The result shows that the delete function cannot be deleted.
the member inherited from the prototype cannot be deleted. copy Code the code is as follows: function flower (){};
flower. prototype. name = "monkey";
var a1 = new flower ();
a1.name = "a1_monkey"
alert (a1.name ); // "a1_monkey"
Delete a1.name; // ture
alert (a1.name); // "monkey"
A1 is a flower instance. Deleting prototype and parent class members through instances is not feasible ~
if you must delete this attribute ("name is used as an example here"), you can only manipulate the prototype.
Delete a1.constructor. prototype. name;
the dontdelete feature Delete cannot delete a member with the dontdelete feature
what is a member with the dontdelete feature?
for example, a variable declared by VAR, A very small number of functions, such as the length of a function object, that have the dontdelete feature
the return value of Delete is false or true
Delete is only used to delete a member that cannot be deleted, false is returned.
in other cases, if a member does not exist, or if the deletion is successful, true is returned.
that is to say, true does not necessarily mean that the deletion is successful.
for example, execute the code alert (delete ); // true
A is an unstated and nonexistent change. Quantity. True still returned by Delete
Differences between browsers copy Code the code is as follows: (function () {
Delete arguments; // false, the return value in Mozilla is true
typeof arguments; // "object"
}) ();
Clever Use of eval to delete var declarative VariablesCopy codeCode: eval ('var flower = 1 ');
Alert (window. Flower) // 1
Alert (flower) // 1
Delete flower; // true
Alert (flower); // "undefined"
VaR A = function (){};
Eval ('var A = function (){}');
Delete A; // true
Alert (a); // "undefined"
The global variables after eval become non-dontdelete and can be deleted using eval;
Finally, I want to add a piece of magic ~ Just tested before going to bed
Window. Flower = 1;
The object does not support this operation when you delete a flower.
We can use with (window) {flower = 1}; then delete flower (remember to delete flower, not delete window. Flower, which IE cannot do)
In this way, window. Flower will be deleted :)
Example of JavaScript Delete Operator