美文网首页
call、apply的用法

call、apply的用法

作者: 蜗牛Coming | 来源:发表于2018-08-29 15:52 被阅读0次

在网上看到的一句话概括,感觉很明了,记录一下
A.functionA.apply(B,[arg1,arg2,...]) === A.functionA.call(B,arg1,arg2,...) ===B.functionA(arg1,arg2,...)
也就是自己没有的属性和方法,去借用别人的方法和属性来执行操作。

一些demo

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

    function Cat(name){
        Animate.apply(this,[name]);
        //等价于
        //Animate.call(this,name);
    }

    var cat = new Cat('miaomiao');
    cat.sayName(); //miaomiao
var arr1 = new Array(1,4,15,9,12,8);
var max = Math.max.apply(Math,arr1);
console.log(max); // 15

相关文章

网友评论

      本文标题:call、apply的用法

      本文链接:https://www.haomeiwen.com/subject/ijckwftx.html