bind 函数实现原理
文章目录
Function.prototype.myBind = function (context) {
var self = this
var args = [].slice.call(arguments, 1)
var Nop = function () { }
var bindfunc = function () {
var bindargs = [].slice.call(arguments)
var finalargs = args.concat(bindargs)
//new 创建的this会只想自己构造函?数对应的实例,如果直接执行this指向window
self.apply(this instanceof self ? this : context, finalargs)
}
//绑定后的函数原型指向 调用方法的原型
Nop.prototype = this.prototype
bindfunc.prototype = new Nop()
return bindfunc
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论