JavaScript 判断对象中是否有某属性

it2022-06-09  88

因为后端返回一个对象后,这个对象经常会出现缺少属性的情况

一个解决方法就是接受到数据后判断对象中是否有该属性,无的话加上属性和默认值

那么如何判断是否有该属性呢:

test.hasOwnProperty('name') //true 自身属性 test.hasOwnProperty('age') //false 不存在 test.hasOwnProperty('toString') //false 原型链上属性

  

使用hasOwnProperty()就很容易知道了。

转载于:https://www.cnblogs.com/piaobodewu/p/10400350.html


最新回复(0)