Issue #79. Correctly setting 'this' in '_.wrap'

This commit is contained in:
Jeremy Ashkenas
2010-12-13 16:25:55 -05:00
parent 9a9f2a85da
commit 3a113d2d88
2 changed files with 6 additions and 1 deletions

View File

@@ -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() {

View File

@@ -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);
};
};