Home > Web Front-end > JS Tutorial > Guide to using hasOwnProperty() method in javascript_Basic knowledge

Guide to using hasOwnProperty() method in javascript_Basic knowledge

WBOY
Release: 2016-05-16 16:10:45
Original
1431 people have browsed it

Overview

hasOwnProperty() method is used to determine whether an object contains the specified own property.

Grammar
obj.hasOwnProperty(prop)

Parameters

•prop

•The name of the attribute to detect.

Description

All objects that inherit Object.prototype will inherit the hasOwnProperty method from the prototype chain. This method can be used to detect whether an object contains specific own properties. Unlike the in operator, this method will ignore those from the prototype. Properties inherited from the chain.

Example

Example 1: Use the hasOwnProperty method to determine whether an object contains specific own properties

The following example checks whether object o contains its own attribute prop:

Copy code The code is as follows:

o = new Object();o.prop = 'exists';function changeO() {
o.newprop = o.prop;
delete o.prop;}o.hasOwnProperty('prop');
// Return true
changeO();
o.hasOwnProperty('prop');
// Return false

Example 2: The difference between own properties and inherited properties

The following example demonstrates the difference between the hasOwnProperty method's treatment of its own properties and inherited properties:

Copy code The code is as follows:

o = new Object();o.prop = 'exists';o.hasOwnProperty('prop'); // Return true
o.hasOwnProperty('toString'); // Return false
o.hasOwnProperty('hasOwnProperty');
// Return false



Example 3: Traverse all self-properties of an object

The following example demonstrates how to ignore inherited properties when traversing all properties of an object. Note that the for..in loop here will only traverse enumerable properties. This is usually what we want. Use Object.getOwnPropertyNames directly. () method can also achieve similar needs.

var buz = {
fog: 'stack'};
for (var name in buz) {
If (buz.hasOwnProperty(name)) {
alert("this is fog (" name ") for sure. Value: " buz[name]);
}
else {
alert(name);
// toString or something else
          }}



Example 4: hasOwnProperty method may be obscured

If an object has its own hasOwnProperty method, the method with the same name on the prototype chain will be shadowed:

var foo = {
HasOwnProperty: function() {
         return false;
},
bar: 'Here be dragons'};foo.hasOwnProperty('bar');
// always returns false
// If you are worried about this situation, you can directly use the real hasOwnProperty method on the prototype chain
({}).hasOwnProperty.call(foo, 'bar');
// true
Object.prototype.hasOwnProperty.call(foo, 'bar');
// true


The above is all the content described in this article, I hope you all like it.
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