SOURCE

console 命令行工具 X clear

                    
>
console
const obj = {
  name: '张三'
}

const foo = function(){
  console.log(this.name)
}
Function.prototype.myCall = function(thisArg, ...args) {
  const fn = Symbol('fn');
  const self = thisArg || window;
  self[fn] = this
  const result = self[fn](args)
  delete self[fn]
  return result
}

Function.prototype.myApply = function(thisArg, args) {
  const fn = Symbol('fn');
  const self = thisArg || window;
  self[fn] = this
  const result = self[fn](...args)
  delete self[fn]
  return result
}

Function.prototype.myBind = function(thisArg, ...args) {
  return () => {
   	this.apply(thisArg, args) 
  }
}



// foo.call(obj)
foo.myCall(obj)
// foo.apply(obj,[])
foo.myApply(obj, [])
// foo.bind(obj)()
foo.myBind(obj)()






callapply、bind 实现
<div>
  call:
  <p>call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数<p>
  语法:function.call(thisArg, arg1, arg2, ...)
</div>
<div style="margin-top: 8px">
  apply:
  <p>apply() 方法调用一个具有给定this值的函数,以及作为一个数组(或类似数组对象)提供的参数。<p>
  语法:func.apply(thisArg, [argsArray])
</div>
<div style="margin-top: 8px">
  bind:
  <p>bind() 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。<p>
  语法:func.bind(thisArg, arg1, arg2, ...)
</div>
p {
  margin: 0;
}