对象.方法();

谁调用该方法this就指向谁、

call()语法:

call()精华:

让一个函数成为指定对象的方法进行调用。

Person.call(document);          //等价于 document.Person();  因此this就指向了括号里的document
//方法.call(对象);

apply():

原理是一样,只是传参的形式不一样

apply是以数组的形式传参(arguments)

 

bind():

有返回值,需要return

let 新函数 = 函数.bind (obj);
//新函数的this。会永久的指向obj,所以就是this被绑定了

 

手动实现一个bind方法:

Function.prototype.bind = function(target){
  var fn = this;
  return function(){
     fn.apply(target , arguments);
  }
}

 伪数组变真数组

       Array.prototype.slice.call();
       ES6中Array.from方法

相关文章:

  • 2021-05-18
  • 2021-05-26
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-01-26
  • 2022-01-12
相关资源
相似解决方案