【发布时间】:2014-04-07 17:35:50
【问题描述】:
我正在阅读Javascript Garden,并试图围绕以下示例进行思考:
传递参数
以下是将参数从一个函数传递到另一个函数的推荐方法。
function foo() { bar.apply(null, arguments); } function bar(a, b, c) { // do stuff here }另一个技巧是同时使用 call 和 apply 来创建快速、未绑定的包装器。
function Foo() {} Foo.prototype.method = function(a, b, c) { console.log(this, a, b, c); }; // Create an unbound version of "method" // It takes the parameters: this, arg1, arg2...argN Foo.method = function() { // Result: Foo.prototype.method.call(this, arg1, arg2... argN) Function.call.apply(Foo.prototype.method, arguments); };
我想弄清楚两件事:
1) 究竟什么是“未绑定包装器”?
2) 从 .call 到 .apply 的链接如何工作和/或使代码更快?
【问题讨论】:
-
其实他想做
Foo.method = Function.call.bind(Foo.prototype.method)
标签: javascript function arguments call apply