JavaScript/call bind apply

来自康健生活
Kangkk​(留言 | 贡献)2020年11月21日 (六) 13:14的版本 (→继承)

引言

call

上下文

继承

bind

bind 实现

Function.prototype.bind = function(newThis) {
  var aArgs  = Array.prototype.slice.call(arguments, 1) //拿到除了newThis之外的预置参数序列
  var that = this
  return function() {
    return that.apply(newThis, aArgs.concat(Array.prototype.slice.call(arguments)))
    //绑定this同时将调用时传递的序列和预置序列进行合并
  }
}