strugglebak / handwrite-bind

手写 bind

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

handwrite-bind

手写 bind

对于 new 来说,它做了 4 步操作

比如如下的代码

var fn = function(a) {
  this.a = a
}

new fn('x')
  • 声明一个临时对象

    var temp = {}
  • 在临时对象上改变它的原型链

    temp.__proto__ = fn.prototype
  • 把临时对象作为 this 传给 fn.call

    fn.call(temp, 'x')
  • fnthis 作为返回值给 return 出来

    return this

About

手写 bind


Languages

Language:JavaScript 100.0%