讲解: 调用函数,等于设置函数体内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

 

相关文章:

  • 2021-05-23
  • 2021-12-13
  • 2021-08-27
  • 2021-12-19
  • 2021-05-30
  • 2021-06-14
猜你喜欢
  • 2021-08-03
  • 2021-06-26
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2021-08-30
  • 2021-08-22
相关资源
相似解决方案