如何判断一个对象的方法是来自这个本身的还是原型的?
function Person() { } Person.prototype.name="Nicholas"; Person.prototype.age=29; Person.prototype.sayName=function(){ alert(this.name); } var person1=new Person(); person1.name="Greg"; var person2=new Person(); console.log(person1.hasOwnProperty("name"));//true console.log(person2.hasOwnProperty("name"));//false console.log("name" in person1);//true console.log("name" in person2);//true for (var prop in person1) { console.log(prop);//name age sayName } function hasPrototypeProperty(object,pro) {//如此可判断存在于原型中的属性 return (!object.hasOwnProperty(pro))&&(pro in object); } console.log(hasPrototypeProperty(person1,"name"));//false console.log(hasPrototypeProperty(person2,"name"));//true
| functionPerson() {}Person.prototype.name="Nicholas";Person.prototype.age=29;Person.prototype.sayName=function(){    alert(this.name);}varperson1=newPerson();person1.name="Greg";varperson2=newPerson();console.log(person1.hasOwnProperty("name"));//trueconsole.log(person2.hasOwnProperty("name"));//falseconsole.log("name"inperson1);//trueconsole.log("name"inperson2);//truefor(varprop inperson1) {    console.log(prop);//name   age   sayName}functionhasPrototypeProperty(object,pro) {//如此可判断存在于原型中的属性    return(!object.hasOwnProperty(pro))&&(pro inobject);}console.log(hasPrototypeProperty(person1,"name"));//falseconsole.log(hasPrototypeProperty(person2,"name"));//true
 | 
原文:http://www.cnblogs.com/yuaima/p/5901557.html