Javascriptdelete is mainly used to delete an attribute from an object or an element from an array. For more information, see the following code. Javascript delete example
The Code is as follows:
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 2
The 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.
The 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.
The Code is as follows:
Window. flower = "monkey ";
Delete flower; // throw an exception
Alert (flower );
In the ff Browser
The 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.
The Code is as follows:
Function flower (){}
Delete flower; // true
Alert (flower); // undefined
The result shows that the delete function cannot be deleted.
Members inherited from the prototype cannot be deleted.
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, and 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 delete feature of DontDelete cannot delete members with the DontDelete feature.
So what is a member with the DontDelete feature?
For example, the variables declared by var, the functions declared by the Function name, and the length of the Function object have the DontDelete feature.
The return value of delete is false or true.
Delete returns false only when deleting a member that cannot be deleted.
In other cases, if a member does not exist or is successfully deleted, true is returned.
That is to say, returning true does not necessarily mean that the deletion is successful.
For example, execute the code alert (delete a); // true
A is an unstated and non-existent variable. Delete still returns true
Differences between browsers
The Code is as follows:
(Function (){
Delete arguments; // false, and true is returned in Mozilla.
Typeof arguments; // "object"
})();
Clever Use of eval to delete var declarative Variables
The Code is 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 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