JavaScript Delete Example
Copy Code code as follows:
var flower={};
Flower.name= "OOP";
Delete Flower.name; True
alert (flower.name); Undefined
Create an object named Flower
Flower has member name, value "OOP";
Delete operation deletes this member
Deletion succeeded, no longer exists flower.name this member
JavaScript Delete Example 2
Copy Code code as follows:
Alert (isNaN (5)); False
Delete isNaN; True
Alert (isNaN (5)); Undefined
The delete operator can even delete members of global object global
Cannot delete variable for VAR declaration
Copy Code code as follows:
var flower= "Monkey";
Delete flower; False
alert (flower); "Monkey"
Variable declared with Var, delete returns false. And did not delete the success variable still exists;
Note: Delete returns false only if you delete a member that cannot be deleted
Under IE cannot delete the variables under the host object under IE browser
Copy Code code as follows:
Window.flower= "Monkey";
Delete flower; Throw an exception
alert (flower);
Under the FF browser
Copy Code code as follows:
Window.flower= "Monkey";
Delete flower; True
Alert (flower)//undefined
Browser performance is inconsistent when you see members of the Delete window
Window is the host object of JavaScript
The host object can be defined by the JavaScript execution environment itself
In the Ie6-8 browser, you cannot delete window.flower, and the browser prompts you to "object does not support the operation", that is, you cannot delete the members of the window
You cannot delete a function that is declared with a function name
Copy Code code as follows:
function Flower () {}
Delete flower; True
alert (flower);//undefined
The result shows that delete cannot delete a function declared with a function name
You cannot delete a member that inherits from a prototype
Copy Code code 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 an instance of flower, and it is not feasible to delete the prototypes and the members of the parent class through an instance.
If you must delete the attribute ("Here for name example"), you can only manipulate the prototype
Delete A1.constructor.prototype.name;
Dontdelete attribute Delete cannot delete a member with a Dontdelete attribute
So what is a member with a dontdelete feature?
For example, Var affirms the variable, function name stated function, length of function object and so on very few have dontdelete characteristic
Delete Returns a value of false or True
Delete returns false only if you delete a member that cannot be deleted
In other cases, deleting a member that does not exist, or the deletion succeeds, will return ture
This means that returning true does not necessarily mean that the deletion succeeded
such as: Execute code alert (delete a); True
A is a variable that is not declared and does not exist. The delete still returns true
Differences between different browsers
Copy Code code as follows:
(function () {
Delete arguments; False, and returns true in Mozilla
typeof arguments; "Object"
})();
Ingenious use of Eval to delete var declaration variables
Copy Code code as follows:
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 variable after eval becomes dontdelete and can be deleted with eval;
Finally add a magic ~ before going to bed just test
Window.flower=1;
The object does not support this action when delete flower
We can use with (window) {flower=1}; Then delete flower (remember to delete flower, not delete window.flower, ie not allowed)
So the window.flower is deleted:
JavaScript Delete Operator Application instance