Javascript中的apply和call继承

call实现继承

call这里call的意思就是把animal的方法应用到cat这个对象身上,也就是animal的属性创建到了cat里面,所以cat就继承了animal的方法


   
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function animal(a, b) {
              this.type = 'animal'
              this.behavior = function(){
                   console.log(this.type+" is running")
              }
         }
         function cat(a, b) {
              this.name = 'wsscat'
              //callanimalcat
              //catanimal
              animal.call(this);
         }
         console.log(new cat())




call实现多重继承

当然我们可以继承多个构造函数,这就是多重继承


   
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function animal(a, b) {
              this.type = 'animal'
              this.behavior = function(){
                   console.log(this.type+" is running")
              }
         }
         function wsscat(a, b) {
              this.age = 0
         }
         function cat(a, b) {
              this.name = 'wsscat'
              //callanimalcat
              //catanimal
              animal.call(this);
              wsscat.call(this);
         }
         console.log(new cat())




只要在cat的构造函数中有多个call就可以,此时的cat继承了wsscat和animal

apply和call的区别

其实apply和call这两个方法基本上是差不多的,区别在于call的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments(即传给构造函数的参数)

例如我们把上面的代码稍微改一下,如果此时我在new构造函数cat的时候传入参数 new cat('wsscat','cute')我们的cat能接收arguments,但是如果此时继承是 animal.call(this),没有给call传第二个参数的时候,生成的对象中type的值就会是undefined,所以为了让这个值能够让animal接收,我们可以在animal中传入第二个参数animal.call(this,type)


   
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function animal(type) {
              this.type = type
              this.behavior = function(){
                   console.log(this.type+" is running")
              }
        }
         function cat(name, type) {
              this.name = name
              //callanimalcat
              //catanimal
              //animal.call(this);//type undefined
              //animal.call(this,type);//type cute
              //animal.call(this,arguments[1]);//type cute
              //animal.call(this,arguments);//type ['wsscat','cute']
              animal.apply(this,arguments)//type: wsscat
         }
         console.log(new cat('wsscat','cute'))


这里用apply就很方便,因为arguments是数组,可以全部传给animal,而call就要一个个地传过去
  1. animal.call(this);//type undefined
  2. animal.call(this,type);//type cute
  3. animal.call(this,arguments[1]);//type cute
  4. animal.call(this,arguments);//type ['wsscat','cute']
  5. animal.apply(this,arguments)//type: wsscat

继承的优化

如果构造函数this绑定太多属性(比如一些共同方法),在实例化后会造成浪费,为此我们一般会使用原型链来优化,但是使用原型链之后我们的apply和call的继承方法就会失效
为此我们一般使用混合的写法,使用原型链和(apply或者call)方法进行继承
具体两句话
让子的原型链指向父的实例(父实例化的对象)
cat.prototype = new animal();
让父的属性创建在子的this上
animal.call(this, type)
整体代码如下,那么就会让父原型链的属性和this上的属性都得到继承


   
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function animal(type) {
              this.type = type
              this.behavior = function() {
                   console.log(this.type + " is running")
              }
         }
         animal.prototype.action = function() {
              console.log("running")
         }
         function cat(name, type) {
              this.name = name
              animal.call(this, type)
         }
         cat.prototype = new animal();
         console.log(new cat('wsscat', 'cute'));
         (new cat('wsscat')).action() //running
转载自:http://www.qdfuns.com/notes/38966/ba2eaa49a9ca6e1bc7a4eee3b34e86b1.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值