当我偶然发现用于创建"快速,未绑定的包装器"的Function.call.apply hack时,我正在浏览JavaScript Garden.它说:
另一个技巧是同时使用call和apply来创建快速,未绑定的包装器.
Run Code Online (Sandbox Code Playgroud)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); };
我不明白的是,当Function.apply足够时,为什么还要使用Function.call.apply.毕竟,它们都在语义上是等价的.
javascript ×1