继承

[javascript]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. </pre><pre name="code" class="javascript">        //1.原型链实现继承  
  2.         function father() {  
  3.             this.faName = 'father';  
  4.         }  
  5.         father.prototype.getfaName = function() {  
  6.             console.log(this.faName);  
  7.         };  
  8.         function child() {  
  9.             this.chName = 'child';  
  10.         }  
  11.         child.prototype = new father();  
  12.         child.prototype.constructor = child;  
  13.         child.prototype.getchName = function() {  
  14.             console.log(this.chName);  
  15.         };  
  16.         /* 
  17.         缺点:1.重写子类的原型 等于 父类的一个实例,(父类的实例属相变成子类的原型属性)如果父类包含引用类型的属性,那么子类所有实例都会共享该属性 
  18.                 (包含引用类型的*原型*属性会被实例共享) 
  19.               2.在创建子类实例时,不能向父类的构造函数传递参数 
  20.          */  
  21.   
  22.         /*--------------------------------------------------------------------*/  
  23.   
  24.         //2.原型连继承和借用构造函数 组合实现继承 (组合继承解决原型链继承的引用类型原型属性被实例共享问题)  
  25.         function father(name) {  
  26.             this.faName = 'father';  
  27.         }  
  28.         father.prototype.getfaName = function() {  
  29.             console.log(this.faName);  
  30.         };  
  31.         function child(args) {  
  32.             this.chName = 'child';  
  33.             father.apply(this,[]); //第二次调用父类构造函数  
  34.         }  
  35.         child.prototype = new father(); //第一次调用父类构造函数  
  36.         child.prototype.constructor = child;  
  37.         child.prototype.getchName = function() {  
  38.             console.log(this.chName);  
  39.         };  
  40.         /* 
  41.         缺点:两次调用父类构造函数:(第一次是在创建子类原型的时候,第二次是在子类构造函数内部) 
  42.               子类继承父类的属性,一组在子类实例上,一组在子类原型上(在子类原型上创建不必要的多余的属性)(实例上的屏蔽原型上的同名属性) 
  43.               效率低 
  44.          */  
  45.   
  46.         /*--------------------------------------------------------------------*/  
  47.   
  48.         //3.寄生组合继承  
  49.         /** 
  50.          * 创建一个拥有指定原型的对象 与Object.create()方法类似 
  51.          * @param  {Object} o [description] 
  52.          */  
  53.         function object(o) {  
  54.             function F() {};  
  55.             F.prototype = o;  
  56.             return new F();  
  57.         }  
  58.         /** 
  59.          * 通用方法实现子类继承父类 
  60.          * @param  {function} child  子类构造函数 
  61.          * @param  {function} father 被继承的父类构造函数 
  62.          */  
  63.         function inheritPrototype(child, father) {  
  64.             var prototype = object(father.prototype); //创建一个指定原型的对象  
  65.             prototype.constructor = child; //增强对象  
  66.             child.prototype = prototype; //子类的原型等于该对象  
  67.         }  
  68.         function father(name) {  
  69.             this.faName = 'father';  
  70.         }  
  71.         father.prototype.getfaName = function() {  
  72.             console.log(this.faName);  
  73.         };  
  74.         function child(args) {  
  75.             this.chName = 'child';  
  76.             father.apply(this,[]);  
  77.         }  
  78.         inheritPrototype(child, father); //子类的原型等于new 空函数(), 而new 空函数()出来的对象的原型等于父类的原型  
  79.         child.prototype.getchName = function() {  
  80.             console.log(this.chName);  
  81.         };  
  82.         console.log( child.prototype.isPrototypeOf(new child()) ); //true  
  83.         console.log(new child() instanceof child); //true  
  84.         /* 
  85.         优点:1.只调用一次父类的构造函数,避免了在子类原型上创建不必要的,多余的属性 
  86.               2.原型链保持不变 
  87.          */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值