动态原型模式 js

动态原型模式
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"  

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

原文地址:https://www.cnblogs.com/cnundefined/p/7111943.html