javascript delete example
var flower={};
flower.name="oop";
delete flower.name; //true
alert(flower.name); //undefined
Create an object named flower
Flower has a member name with value "oop";
The delete operation deletes this member
The deletion is successful and the member flower.name no longer exists
javascript delete example 2
alert(isNaN(5)); //false
delete isNaN; //true
alert(isNaN(5)); //undefined
The delete operator can even delete members of the global object Global
cannot delete variables declared by var
var flower="monkey";
delete flower; //false
alert( flower); // "monkey"
Variables declared with var will return false after delete. The variable still exists if the deletion is not successful;
Note: delete only deletes a member that cannot be deleted. Yes, it will return false
Variables under the host object cannot be deleted under IE browser
window.flower="monkey";
delete flower; //Throws an exception
alert(flower);
Browse in ff Copy the code under the device
window.flower="monkey";
delete flower; //true
alert(flower) //undefined
When you can see the members of delete window, the browser behaves inconsistently
Window is the host object of javascript
The host object can be defined by the JavaScript execution environment
In the IE6-8 browser, window.flower cannot be deleted. The browser will prompt you that "the object does not support this operation", that is, the members under window cannot be deleted
You cannot delete a function declared with a function name
function flower( ){}
delete flower; //true
alert(flower);//undefined
The result shows that delete cannot delete the function declared with the function name
cannot delete the function inherited from Members of the prototype
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. To delete members of the prototype and parent class through the instance is Not feasible~
If you must delete this attribute ("here, take name as an example"), you can only manipulate the prototype
delete a1.constructor.prototype.name;
DontDelete attribute delete cannot be deleted. Members with the DontDelete attribute
So what are members with the DontDelete attribute?
For example, variables declared by var, functions declared by function names, lengths of Function objects, etc., are very few that have the DontDelete attribute
delete return value false or true
delete will return false
only when deleting a member that cannot be deleted. In other cases, deleting a non-existing member or deleting it successfully will also return true
, which means it returns true. It does not necessarily mean that the deletion is successful
For example: executing code alert(delete a); // true
a is an undeclared variable that does not exist. delete still returns true
Differences between different browsers
(function(){
delete arguments; // false , while in Mozilla it returns true
typeof arguments; // "object"
})();
Cleverly use eval to delete the variables declared by var
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 does not have the DontDelete feature and can be deleted with eval;
Finally add a magical thing ~ just before going to bed When testing
window.flower=1;
delete flower the object will not support this operation
We can use with(window){flower=1}; and then delete flower (remember it is delete flower, not delete window.flower, IE does not allow that)
In this way, window.flower will be deleted:)
JavaScript delete operator application example