Issue #243. _.functions now also returns the names of function defined in the prototype chain.

This commit is contained in:
Jeremy Ashkenas
2011-07-13 11:09:51 -04:00
parent 727db393d5
commit cd236d4c47
2 changed files with 9 additions and 1 deletions

View File

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

View File

@@ -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).