JS中的call()、apply() 以及 bind()方法用法总结

JS中的call()方法和apply()方法用法总结  :

讲解: 调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域。

    function add(c,d){
        return this.a + this.b + c + d;
    }

    var s = {a:1, b:2};
    console.log(add.call(s,3,4)); // 1+2+3+4 = 10
    console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14 

上面的例子中   add.call(s,3,4)   语句,执行add函数通过call把add内的this指向了参数中的  s对象,这样add函数作用域中的 this下的变量  都可以从 s对象下的作用域 中获取。

    参考   :     https://blog.csdn.net/ganyingxie123456/article/details/70855586 

改变this的三种方式:call,apply,bind简述:https://blog.csdn.net/qq_42451932/article/details/86256101

原文地址:https://www.cnblogs.com/wfblog/p/9169026.html