>》>》>》>》

call,apply和bind的秒懂区别

对象.方法();

谁调用该方法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方法

posted @ 2019-07-19 19:12  大、锤  阅读(973)  评论(1编辑  收藏  举报