new

  1. function defineNew(fn, ...args) {
  2. const obj = {}
  3. obj.__proto__ = fn.prototype
  4. fn.apply(obj, args)
  5. return obj
  6. }

call

  1. Function.prototype.gromyCall = function(target,...args){
  2. //此时this是调用者,Function
  3. let context = Symbol('this') //避免key冲突
  4. target[context] = this //把调用者的作用域地址送给目标的一个属性
  5. return target[context](...args) //执行并返回
  6. }

apply

  1. Function.prototype.gromyApply = function(target,args){
  2. let context = Symbol('this');
  3. target[context] = this
  4. return target[context](...args)
  5. }

bind

  1. Function.prototype.gromyBind = function(target,...args){
  2. let context = Symbol('this')
  3. target[context] = this
  4. let result = function(){
  5. return target[context](...args)
  6. }
  7. if(this.prototype){ //保证
  8. result.prototype = this.prototype
  9. }
  10. return result
  11. }