动态原型模式

function Person1(name,age){
        this.name = name;
        this.age = age;
        if (typeof this.sayName != 'function')
        {
            Person1.prototype.sayName = function(){
                console.log(this.name);
            }
        }
    }
function Person1(name,age){
        this.name = name;
        this.age = age;
        if (typeof this.sayName != 'function')
        {
            Person1.prototype = {
                sayName:function(){
                    console.log(this.name);
                }
            }
        }
    }

上边和下边不一样,下边会断开以前的原型的指针,因为{}就是一个对象

猜你喜欢

转载自www.cnblogs.com/jokes/p/9275289.html