1

In Javascript, if an object has lots of properties that are functions:

var obj = { foo: function() { ... },
            bar: function() { ... },
              ...
          }

then how can you get an array of names of those functions? That is, an array

["foo", "bar", ... ]

thanks.

5 Answers 5

5
var names = [];
for( var k in obj ) {
   if(obj.hasOwnProperty(k) && typeof obj[k] == 'function') {
      names.push(k);
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

interesting, how come push will not push a function reference into the array but will push the name of the function?
for in loops over property names, not the actual properties. So if you want to reference each property, you would use obj[k].
so what if there is only 1 function inside, still have to use a "for in" loop to achieve this purpose? is there another way if "for in" loop doesn't exist?
to enumerate properties you have to use for..in. There is no other correct way.
3
var functions = [];
for (var prop in obj) {
    if ((typeof obj[prop]) == 'function') {
        // it's a function
        functions.push(prop);
    }
}

Comments

3

Edit: I've slightly misread the question, you want to extract the names of only the properties that are function objects:

function methods(obj) {
  var result = [];
  for (var prop in obj) {
    if (obj.hasOwnProperty(prop) && typeof obj[prop] == 'function') {
      result.push(prop);
    }
  }
  return result;
}

var obj = {
  foo: function() { },
  bar: function() { },
};

methods(obj); // ["foo", "bar"]

I'm using the hasOwnProperty method, to ensure that the enumerated properties in fact exist physically in the object.

Note that this approach and all other answers have a small problem IE.

The JScript's DontEnum Bug, custom properties that shadow non-enumerable properties (DontEnum) higher in the prototype chain, are not enumerated using the for-in statement, for example :

var foo = {
  constructor : function() { return 0; },
  toString : function() { return "1"; },
  valueOf : function() { return 2; }
  toLocaleString : function() { return "3"; }
};

for (var propName in foo ) { alert(propName); }

The object foo clearly has defined four own properties, but those properties exist in Object.prototype marked as DontEnum, if you try to enumerate the properties of that object with the for-in statement in IE, it won't find any.

This bug is present on all IE versions, and has been recently fixed in IE9 Platform Preview.

1 Comment

+1 for mentioning hasOwnProperty, but I believe Jian was after just the functions, not all the properties.
1

To complete other answers: you can also use instanceof:

var obj = { foo: function() { ... },
            bar: function() { ... },
              ...
          },
    fnArr = [];

for (var label in obj){
  if (obj[label] instanceof Function){
     fnArr.push(label)
  }
}

Comments

1

With ES5:

var obj = {
    foo: function() {},
    bar: function() {},
    baz: true
};

function getMethods(object) {
    return Object.keys(object).filter(function(key) {
        return typeof object[key] == 'function';
    });
}

getMethods(obj); // [foo, bar]

Object.keys(<object>) returns the names of all enumerable properties of an object as an array, of which the non-functions are filtered out.

Example - works on Chrome release and nightly builds of Webkit and Tracemonkey (Firefox).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.