From 3a113d2d886ade3f83e8e49bb144a877a939062e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Mon, 13 Dec 2010 16:25:55 -0500 Subject: [PATCH] Issue #79. Correctly setting 'this' in '_.wrap' --- test/functions.js | 5 +++++ underscore.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/functions.js b/test/functions.js index 61ab2ce4a..3c9f6a2c1 100644 --- a/test/functions.js +++ b/test/functions.js @@ -96,6 +96,11 @@ $(document).ready(function() { var greet = function(name){ return "hi: " + name; }; var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); }); equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function'); + + var inner = function(){ return "Hello "; }; + var obj = {name : "Moe"}; + obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; }); + equals(obj.hi(), "Hello Moe"); }); test("functions: compose", function() { diff --git a/underscore.js b/underscore.js index 75953925c..6cb2527dd 100644 --- a/underscore.js +++ b/underscore.js @@ -469,7 +469,7 @@ _.wrap = function(func, wrapper) { return function() { var args = [func].concat(slice.call(arguments)); - return wrapper.apply(wrapper, args); + return wrapper.apply(this, args); }; };