玩笑过后

导航

js中对象继承的冒充方法

function Parent(name){
    this.name = name;
    this.sayName  = function(){
       console.log(this.name); 
   }
}

function Son(name,password){
  this.method = Parent;
  this.method(name);
  this.show = function(){
       console.log(this.name+' : '+this.password)  
   }    
  delete this.method;
}

var son1 = new Son('zhangsan','123456');
son1.show();

这种继承方式和原型链继承不一样,

这个是偷换了this的指向

posted on 2018-06-25 12:21  玩笑过后  阅读(142)  评论(0编辑  收藏  举报