
1.通过in方法判断:
in方法后面跟的是一个对象,判断的是对象的自有属性和继承属性中是否有该属性值。若有返回true,否则返回false.代码如下:


2.通过对象的hasOwnProperty()方法判断:
方法接受属性参数,返回true或false,若输入属性为对象的自有属性则返回true,否则返回false.一般在遍历对象属性的时候通过该方法过滤掉从原型链上继承下来的属性。


3.tips:
可通过Object.keys("对象")返回对象的所有属性,返回的是对象的自有属性的集合,返回为对象属性组成的数组。
网友评论