javascript的继承方式

常用的方法
 
比如,现在有一个“动物”对象的构造函数。
function Animal(){
    this.species = "动物";
  }

 

还有一个“猫”对象的构造函数。

function Cat(name,color){
 
    this.name = name;
 
    this.color = color;
 
  }

  

 
1.绑定构造函数继承
  function Cat(name,color){
 
    Animal.apply(this, arguments);
 
    this.name = name;
 
    this.color = color;
 
  }
 
  var cat1 = new Cat("大毛","黄色");
 
  alert(cat1.species); // 动物

  

2.原型链继承
1 Cat.prototype = new Animal();
2  
3   Cat.prototype.constructor = Cat;
4  
5   var cat1 = new Cat("大毛","黄色");
6  
7   alert(cat1.species); // 动物

 

3.object方法继承
function object(o) {
 
    function F() {}
 
    F.prototype = o;
 
    return new F();
  }
  var Doctor = object(Chinese);
 
  Doctor.career = '医生';
 
  alert(Doctor.nation); //中国
 

 

 
另外一些方式
 
如拷贝继承、直接继承prototype、使用空对象作为中介继承等
 
详见
 
 
 

转载于:https://www.cnblogs.com/summer323/p/5280025.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值