mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Issue #79. Correctly setting 'this' in '_.wrap'
This commit is contained in:
@@ -96,6 +96,11 @@ $(document).ready(function() {
|
|||||||
var greet = function(name){ return "hi: " + name; };
|
var greet = function(name){ return "hi: " + name; };
|
||||||
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
|
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
|
||||||
equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
|
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() {
|
test("functions: compose", function() {
|
||||||
|
|||||||
@@ -469,7 +469,7 @@
|
|||||||
_.wrap = function(func, wrapper) {
|
_.wrap = function(func, wrapper) {
|
||||||
return function() {
|
return function() {
|
||||||
var args = [func].concat(slice.call(arguments));
|
var args = [func].concat(slice.call(arguments));
|
||||||
return wrapper.apply(wrapper, args);
|
return wrapper.apply(this, args);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user