# js 中如何实现 bind
Issue
欢迎在 Issue 中交流与讨论: Issue 32 (opens new window)
Author
最简单的 bind
一行就可以实现,而在实际面试过程中也不会考察你太多的边界条件
Function.prototype.fakeBind = function(obj) {
return (...args) => this.apply(obj, args)
}
测试一下
function f (arg) {
console.log(this.a, arg)
}
// output: 3, 4
f.bind({ a: 3 })(4)
// output: 3, 4
f.fakeBind({ a: 3 })(4)