Check if a property exists in an object

It is easy to meet to judge whether there is an object in the object, and one of the writing methods will be used below.

// 判断对象是否存在某一个属性,正确写法
const hasProperty = (obj,key) => key in obj;

What are common typos?

The first:

// 这只是判断了obj是否存在key这个属性,
const hasProperty = (obj,key) => obj.key !==undefined

// 而我们需要判断的应该是它的值,所以应该这样写
const hasProperty = (obj,key) => obj[key] !==undefined

Is it ok to write like this? Can you use undefined to determine whether an object property exists? Enter this piece of code in the browser control, press Enter to run. Let's verify that the first way of writing is correct:

const hasProperty=(obj,key) => obj[key] !==undefined;
var obj={k:undefined};
hasProperty(obj,'k');

When the object property value is undefined, it returns false, and it is expected to be true (k is stored in obj), so undefined cannot be used to determine whether a certain property exists in the object.

The second type:

// 通过Object.keys(obj)得到属性形成的属性数组['key'],再用includes()判断['key']是否有参数key的值
const hasProperty = (obj,key) => Object.keys(obj).includes(key);

Is that possible? verify:

const hasProperty = (obj,key) => Object.keys(obj).includes(key);
var obj={k:undefined, o:2};
hasProperty(obj,'o');// ['k','o'],'o'

// true

 This seems to be able to judge. What if the incoming object looks like this?

const hasProperty = (obj,key) => Object.keys(obj).includes(key);
var obj={k:undefined, o:2};

// 给obj对象加一个属性p和其值为1
Object.defineProperty(obj,'p',{
    enumerable: false, // 不可遍历
    value: 1, // p 的值
})

console.log(obj.p); // 1 

hasProperty(obj,'p'); // false

From the figure below, we find that obj already exists p and p also has a value, but it cannot be judged by hasProperty(), so the second method does not work.

 The third type:

The third is hasOwnProperty(), a method provided by the object to determine whether the object has a property. This function only checks whether the object itself has this property. Object.prototype.hasOwnProperty.call(obj, key) is the same as hasOwnProperty(), because obj can get the method of Object from the prototype chain.

const hasProperty = (obj,key) =>obj.hasOwnProperty(key);
var obj={k:undefined, o:2};

// 给obj对象加一个属性p和其值为1
Object.defineProperty(obj,'p',{
    enumerable: false, // 不可遍历
    value: 1, // p 的值
})

console.log(obj.p); // 1 

hasProperty(obj,'p'); // true

Run the console, hasOwnProperty() judges that there is no problem with the object itself, and the verification finds that the judgment of hasProprty() is inconsistent with the expected result. But we know that obj has a toString() method, so where does toString() come from? toString() comes from the prototype chain. If the obj object itself does not exist, obj will be found from the prototype chain.

console.log(obj.toString) // ƒ toString() { [native code] }

hasProperty(obj,'toString'); // false

Prototype chain, you can refer to this picture:

So how to judge whether an object has a certain property and can find toString() from the prototype chain? You can use in:

const hasProperty = (obj,key) => key in obj;


var obj={k:undefined, o:2};

// 给obj对象加一个属性p和其值为1
Object.defineProperty(obj,'p',{
    enumerable: false, // 不可遍历
    value: 1, // p 的值
})

console.log(obj.p); // 1 

hasProperty(obj,'p'); // true


hasProperty(obj,'toString'); // true

5.17 EhO:/ Determine whether a certain attribute exists in a certain object, check common error scenarios, have you been tricked? #web Front End Interview Questions# javascript# es6# Programmer https://v.douyin.com/S5pWyCL/ Copy this link, open Douyin search, and watch the video directly! https://v.douyin.com/S5pWyCL/

Guess you like

Origin blog.csdn.net/qq_58062502/article/details/129410561