1 通过hasOwnProperty()//检测一个属性是自身对象,还是原型链上的
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.sex = '男';
var p = new Person('张三',20)
p.weight = 120;
p.height = 180;
console.log(p) //Person {name: '张三', age: 20, weight: 120, height: 180}
console.log(p.hasOwnProperty('weight')); //true(属于自身的)
console.log(p.hasOwnProperty('sex')); //false(属于原型链上的)
for(let i in p){
console.log(p[i]) //张三, 20, 120, 180, 男
}
for(let i in p){
if(p.hasOwnProperty(i)){ //原型链上的值是不能打印的 如:‘男’
console.log(p[i]) //张三, 20, 120, 180
}
}