name属性要么是直接在对象上访问到的,要么是通过原型访问到的。因此调用name in person始终都返回true,无论该属性存在于实例还是存在于原型中。hasOwnProperty()只有在实例中才会返回true。

  function hasPrototypeProperty(object, name){

  return !object.hasOwnProperty(name) && (name in object);

}

如果函数返回真,说明原型中有这个属性。否则说明属性在实例中,或者不存在此属性。

function Person(){

  Person.prototype.name = "Nicholas";

  Person.prototype.sayname = function(){

  alert(this.name);

  }

}

var persion = new person();

alert(hasPrototypeProperty(person,name));//true

person.name = "xupen";

alert(hasPrototypeProperty(person,name));//false

相关文章:

  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-02-01
猜你喜欢
  • 2021-10-01
  • 2021-07-19
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案