JavaScript关于原型的相关内容

function Person () {
}
Person.prototype.name = 'Alan';
Person.prototype.age = 26;
Person.prototype.job = 'Software Engineer';
Person.prototype.sayName = function () {
  alert(this.name);  
}

var person1 = new Person();
var person2 = new Person();

alert(person1.hasOwnProperty('name');  //false

person1.name = 'Ronaldo';
alert(person1.name); //Ronaldo
alert(person1.hasOwnProperty('name'); //true
alert('name' in person1); //true

alert(person2.name) //Alan;
alert(person2.hasOwnProperty('name'); //false
alert('name' in person2); //true
//判断是否是原型属性
function hasPrototypeProperty(obj, name) {
  return !obj.hasOwnProperty(name) && (name in obj);
}

  

posted @ 2017-03-22 10:51  halapro.liu  阅读(79)  评论(0编辑  收藏  举报