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')
-
把
fn
的this
作为返回值给return
出来return this