201511041227_《JavaScript——动态原型对象(优化写法)》

function Person(userName ,age){
    this.userName = userName ;
    this.age = age;
    this.family = ['爸爸','妈妈','姐姐','老婆'];
    if(typeof this.say !== 'function'){
    Person.prototype.say = function(){
        return this.userName + this.age + '我回来了~';  
         };
    }
};

var xiaoYin = new Person('殷敏峰',34);
console.log(xiaoYin.say());  //殷敏峰34我回来了~

var xiaoJuan = new Person('殷敏娟',36);
console.log(xiaoJuan.say());  //殷敏娟36我回来了~

 

你可能感兴趣的:(201511041227_《JavaScript——动态原型对象(优化写法)》)