Home > Web Front-end > JS Tutorial > Use of JavaScript delete attribute_javascript tips

Use of JavaScript delete attribute_javascript tips

WBOY
Release: 2016-05-16 18:45:14
Original
1181 people have browsed it

delete is to delete an attribute of an object. For example, for an object,
var obj = {key:5};
delete obj.key is to delete the key attribute of the object. This is no problem, but when the prototype of the object It is worth noting when this attribute also exists on the object.

Copy code The code is as follows:

var A = function(){};
A.prototype.testMe = true;
var a = new A();
//Override prototype properties
a.testMe = true;
if(a.testMe){
/ / Some key codes...
// ....
//Delete this attribute
delete a.testMe;
}
//Second paragraph ------ ---------------------
// In other modules
if(a.testMe){
// Some key codes.. .
// ....
}

The second paragraph is worth noting. Don’t think that testMe in a no longer exists after it has been deleted, so a.testMe If it is undefined, it is false. In fact, it still exists through prototype access, and it is still true!
I got tricked here if I didn't pay attention.
//Attachment:
Detect whether an object has a certain attribute, including the prototype chain:
if ('attrName' in obj)...
Detect whether an object has a certain attribute, which belongs to the object itself, and Non-prototype chain:
obj.hasOwnProperty('attrName')
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