Discuss / JavaScript / 小白不太懂,求解

小白不太懂,求解

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

要过滤掉对象继承的属性,用hasOwnProperty()来实现:

var o = {
    name: 'Jack',
    age: 20,
    city: 'Beijing'
};
for (var key in o) {
    if (o.hasOwnProperty(key)) {
        console.log(key); // 'name', 'age', 'city'
    }
}

请问这个例子怎么理解,什么叫“对象继承的属性;我看得出的结果跟不用”hasOwnProperty()"好像没有区别

#2 Created at ... [Delete] [Delete and Lock User]

懂了懂了,闲着没事复习前面的时候,发现对象那里提到过,打扰了


  • 1

Reply