Home >Web Front-end >JS Tutorial >javascript delete usage sample code

javascript delete usage sample code

高洛峰
高洛峰Original
2017-01-20 10:52:051447browse

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 the value "oop";
The delete operation deletes this member
The deletion is successful and no longer exists The member flower.name
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 with var

var flower="monkey"; 
delete flower; //false 
alert(flower); // "monkey"

Those declared with var Variables, return false after delete. The variable still exists if it is not deleted successfully;
Note: delete will only return false when deleting a member that cannot be deleted
Variables under the host object cannot be deleted under IE. When browsing in IE Under the browser

window.flower="monkey"; 
delete flower; //抛出异常 
alert(flower);

Under the ff browser

window.flower="monkey"; 
delete flower; //true 
alert(flower) //undefined

When you can see the members of delete window, the browser behaves inconsistently
The window is the host object of javascript
The host object can be The javascript execution environment is defined by itself
In the IE6-8 browser, you cannot delete window.flower. The browser will prompt you that "the object does not support this operation", that is, you cannot delete members under the window
You cannot delete using functions The function declared with the 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 members inherited from 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, and the prototype is deleted through the instance and members of the parent class are 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 delete 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
That is to say, returning true 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 ,而在Mozilla中返回的是true 
typeof arguments; // "object" 
})();

Smart use of 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 variables after eval become unavailable DontDelete feature, you can use eval to delete;
Finally add a magical thing~Just tested before going to bed
window.flower=1;
The object will not support this operation when deleting flower
We can use with( window){flower=1}; 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:)

Update For articles related to multiple javascript delete usage sample codes, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn