SOURCE

var user = {
  greet: "Hello!",
  greetUser: function(username){
    return this.greet+" "+ username;
  }
}

console.log(user.greetUser("aa"))
var fun = user.greetUser.bind(user);
var fun1 = user.greetUser.bind({greet: "qin"})
console.log(fun("bb"))
console.log(fun1("chuan"))
console.log('---------------------------------')
var user2 = {
    greet : 'Hello2'
}
console.log(user.greetUser.call(user2))
console.log(user.greetUser.call(user2, 'dd'))
console.log(user.greetUser.apply(user2, ['ee']))
Function.prototype.mycall = function (context) {
  if (typeof this !== 'function') {
    throw new TypeError('not funciton')
  }
  context = context || window
  context.fn = this
  let arg = [...arguments].slice(1)
  let result = context.fn(...arg)
  delete context.fn
  return result
} 

Function.prototype.myapply = function (context) {
  if (typeof this !== 'function') {
    throw new TypeError('not funciton')
  }
  context = context || window
  context.fn = this
  let result
  if (arguments[1]) {
    result = context.fn(...arguments[1])
  } else {
    result = context.fn()
  }
  delete context.fn
  return result
}

Function.prototype.mybind = function (context) {
  if (typeof this !== 'function') {
    throw new TypeError('Error')
  }
  let _this = this
  let arg = [...arguments].slice(1)
  return function F() {
    // 处理函数使用new的情况
    if (this instanceof F) {
      return new _this(...arg, ...arguments)
    } else {
      return _this.apply(context, arg.concat(...arguments))
    }
  }
}
console.log(user.greetUser.mycall(user2, 'ff'))
console 命令行工具 X clear

                    
>
console