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('aa',34);
console.log(xiaoYin.say());  //aa34我回来了~

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

 

posted @ 2015-11-04 12:30  Coca-code  阅读(150)  评论(0编辑  收藏  举报