From 5ca27ffed139bb14aad12040c05f9b88a7ab8597 Mon Sep 17 00:00:00 2001 From: Bob Remeika Date: Tue, 20 Dec 2011 01:51:51 -0800 Subject: [PATCH] Fixes #409 - Unexpected arguments passed to wrapper function in _.wrap when the first argument of calling the wrapper function is an array --- test/functions.js | 5 +++++ underscore.js | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/functions.js b/test/functions.js index fdaddcc96..78721af16 100644 --- a/test/functions.js +++ b/test/functions.js @@ -163,6 +163,11 @@ $(document).ready(function() { var obj = {name : "Moe"}; obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; }); equals(obj.hi(), "Hello Moe"); + + var noop = function(){}; + var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); }); + var ret = wrapped(['whats', 'your'], 'vector', 'victor'); + same(ret, [noop, ['whats', 'your'], 'vector', 'victor']); }); test("functions: compose", function() { diff --git a/underscore.js b/underscore.js index 9cf1de923..638ae6a6c 100644 --- a/underscore.js +++ b/underscore.js @@ -25,7 +25,6 @@ // Create quick reference variables for speed access to core prototypes. var slice = ArrayProto.slice, - concat = ArrayProto.concat, unshift = ArrayProto.unshift, toString = ObjProto.toString, hasOwnProperty = ObjProto.hasOwnProperty; @@ -584,7 +583,7 @@ // conditionally execute the original function. _.wrap = function(func, wrapper) { return function() { - var args = concat.apply([func], arguments); + var args = [func].concat(slice.call(arguments, 0)); return wrapper.apply(this, args); }; };