动态原型模式
function Person(name,age){
    this.name = name;
    this.age = age;
    if(typeof this.sayName != "function"){
        Person.prototype.sayName = function(){
            alert(this.name);
        }
    }
}

var person1 = new Person("china",2);
person1.sayName();  //"china"  

注:使用动态原型模式时,不能使用对象字面量重写原型,如果在已经创建了实例的情况下重写原型,那么就会切断现有实例与新原型之间的联系

相关文章:

  • 2021-12-23
  • 2022-12-23
  • 2021-07-31
  • 2021-11-19
  • 2022-12-23
猜你喜欢
  • 2021-06-19
  • 2022-12-23
  • 2021-11-26
  • 2021-10-01
  • 2022-01-31
  • 2021-07-19
  • 2022-02-16
相关资源
相似解决方案