mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
_.result calls property with the correct context.
This commit is contained in:
@@ -166,10 +166,10 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('result calls functions and returns primitives', function() {
|
test('result calls functions and returns primitives', function() {
|
||||||
var obj = {w: '', x: 'x', y: function(){ return 'y'; }};
|
var obj = {w: '', x: 'x', y: function(){ return this.x; }};
|
||||||
strictEqual(_.result(obj, 'w'), '');
|
strictEqual(_.result(obj, 'w'), '');
|
||||||
strictEqual(_.result(obj, 'x'), 'x');
|
strictEqual(_.result(obj, 'x'), 'x');
|
||||||
strictEqual(_.result(obj, 'y'), 'y');
|
strictEqual(_.result(obj, 'y'), 'x');
|
||||||
strictEqual(_.result(obj, 'z'), undefined);
|
strictEqual(_.result(obj, 'z'), undefined);
|
||||||
strictEqual(_.result(null, 'x'), null);
|
strictEqual(_.result(null, 'x'), null);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -878,7 +878,7 @@
|
|||||||
_.result = function(object, property) {
|
_.result = function(object, property) {
|
||||||
if (object == null) return null;
|
if (object == null) return null;
|
||||||
var value = object[property];
|
var value = object[property];
|
||||||
return _.isFunction(value) ? value() : value;
|
return _.isFunction(value) ? value.call(object) : value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add your own custom functions to the Underscore object, ensuring that
|
// Add your own custom functions to the Underscore object, ensuring that
|
||||||
|
|||||||
Reference in New Issue
Block a user