简单实现 Function.bind 函数
if (!Function.prototype.bind) {
Function.prototype.bind = function(that) {
var func = this, args = arguments;
return function() {
return func.apply(that, Array.prototype.slice.call(args, 1));
}
}
}
// 只支持 bind 阶段的默认参数:
func.bind(that, arg1, arg2)();
// 不支持以下调用阶段传入的参数:
func.bind(that)(arg1, arg2);
JavaScript:理解与实现Function.bind
本文探讨了JavaScript中Function.prototype.bind方法的工作原理,并提供了一个简单的实现方式,帮助理解其核心概念。
395

被折叠的 条评论
为什么被折叠?



