标签:
var Person = function(){
this.name = "nike";
this.age = "20"
}
var person = new Person();
for(v in person1){
console.log(person1);
}
//nike 20
Person.prototype.weight = 50;
for(v in person1){
console.log(person1);
}
//nike 20 50
for(v in person1){
if(person1.hasOwnProperty(v)){
console.log(person1);
}
}
//nike 20
ps:hasOwnProperty 只是判断对象本身是否有属性值,而不去原型链上去找,而for in循环会找自身和原型链上,为了避免用for in时参杂原型链上的属性,用hasOwnProperty来判断。让代码更干净!
标签:
原文地址:http://www.cnblogs.com/biangz/p/5651035.html