From cd236d4c47d296d20ae606e8a33ccc25ccd1188e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 13 Jul 2011 11:09:51 -0400 Subject: [PATCH] Issue #243. _.functions now also returns the names of function defined in the prototype chain. --- test/objects.js | 4 ++++ underscore.js | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/test/objects.js b/test/objects.js index 48971c079..78add1fed 100644 --- a/test/objects.js +++ b/test/objects.js @@ -22,6 +22,10 @@ $(document).ready(function() { test("objects: functions", function() { var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce}; ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object'); + + var Animal = function(){}; + Animal.prototype.run = function(){}; + equals(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype'); }); test("objects: extend", function() { diff --git a/underscore.js b/underscore.js index 5757609fa..9857159eb 100644 --- a/underscore.js +++ b/underscore.js @@ -559,7 +559,11 @@ // Return a sorted list of the function names available on the object. // Aliased as `methods` _.functions = _.methods = function(obj) { - return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort(); + var names = []; + for (var key in obj) { + if (_.isFunction(obj[key])) names.push(key); + } + return names.sort(); }; // Extend a given object with all the properties in passed-in object(s).