From ebb9db4e8e62a8760d8d24cbda6b7c66dc211d0e Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Wed, 21 Mar 2012 06:33:37 -0400 Subject: [PATCH] _.result calls `property` with the correct context. --- test/utility.js | 4 ++-- underscore.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/utility.js b/test/utility.js index ec9960eae..da374b59d 100644 --- a/test/utility.js +++ b/test/utility.js @@ -166,10 +166,10 @@ $(document).ready(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, 'x'), 'x'); - strictEqual(_.result(obj, 'y'), 'y'); + strictEqual(_.result(obj, 'y'), 'x'); strictEqual(_.result(obj, 'z'), undefined); strictEqual(_.result(null, 'x'), null); }); diff --git a/underscore.js b/underscore.js index c699c991b..aeaa8b1b0 100644 --- a/underscore.js +++ b/underscore.js @@ -878,7 +878,7 @@ _.result = function(object, property) { if (object == null) return null; 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