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

javascript delete usage sample code_basic knowledge

WBOY
Release: 2016-05-16 18:31:14
Original
956 people have browsed it

javascript delete example

Copy code 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 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
Copy code 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 Global
cannot delete variables declared by var
Copy code The code is as follows:

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
Copy code Code As follows:

window.flower="monkey";
delete flower; //Throws an exception
alert(flower);

Browse in ff Copy the code under the device
The code is as follows:

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
Copy code The code is as follows:

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
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 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
Copy code The code is as follows:

(function(){
delete arguments; // false , while in Mozilla it returns true
typeof arguments; // "object"
})();

Cleverly use eval to delete the variables declared by var
Copy the code 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 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
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template