About javascript Object. hasOwnProperty, it's enough for me

Keywords: Javascript Attribute

Basic concepts of hasOwnProperty

The hasOwnProperty() method returns a Boolean value indicating whether the specified property exists in the object's own property (non inherited property),
If the object has a property with the specified name, the hasOwnProperty method returns true, otherwise false. This method does not check properties in the object prototype chain; the property must be a member of the object itself.

Using grammar

obj.hasOwnProperty(prop)

parameter

obj, required. Object.
prop, required. The string value of a property name.

demo

Judge whether the attribute exists

//Instantiate an object
const obj = new Object();
//Add attribute name for obj
obj.name = "Cold in the streets";
//Add attribute sex for obj
obj.sex="male"

const a = obj.hasOwnProperty('name');
console.log(a);// true
//Delete the name attribute of obj
delete obj.name
const b = obj.hasOwnProperty('name');
console.log(b); // false
const c = obj.hasOwnProperty('sex');
console.log(c); //  true

Cannot judge inherited property through obj.hasOwnProperty(prop)

obj= new Object();
obj.name = 'Cold in the streets';
const a = obj.hasOwnProperty('name');
console.log(a);//true
const b = obj.hasOwnProperty('toString');
console.log(b);//false
const c = obj.hasOwnProperty('hasOwnProperty');
console.log(c);//false

If you want to judge the inherited attribute, you can judge it by prototype chain

const d = Object.prototype.hasOwnProperty('toString')
console.log(d);//true
const e = String.prototype.hasOwnProperty('split')
console.log(e);//true

Traversing all the properties of an object

Loop through all enumeration properties of the object through for...in, and then use the hasOwnProperty() method to ignore inherited properties.
Another way of writing

const obj ={
    name:"Cold in the streets",
    sex:"male"
}
for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
        console.log(`${key}: ${obj[key]}`)
    }
    else 
        console.log(key); 
    }
}

output

JavaScript does not protect the hasOwnProperty property name. There may be a pit when it is used

const foo = {
    hasOwnProperty: function() {
        return false;
    },
    bar: 'It's a pit, and it may return forever false'
};
const hasBar = foo.hasOwnProperty('bar'); 
console.log(hasBar);// Always return false

// If you are worried about this situation, you can directly use the real hasOwnProperty method on the prototype chain
const a = ({}).hasOwnProperty.call(foo, 'bar'); // true
console.log(a);
// You can also use the hasOwnProperty property on the Object prototype
const b = Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
console.log(b);

Original address
Reference link:
Object.prototype.hasOwnProperty()")
hasOwnProperty method (Object) (JavaScript) . aspx "hasOwnProperty method (Object) (JavaScript)")
The hasOwnProperty method of js property object

Posted by dashti on Wed, 11 Dec 2019 13:06:05 -0800