How to get an object's methods? How to get an object's methods? javascript javascript

How to get an object's methods?


function getMethods(obj){    var res = [];    for(var m in obj) {        if(typeof obj[m] == "function") {            res.push(m)        }    }    return res;}


Remember that technically javascript objects don't have methods. They have properties, some of which may be function objects. That means that you can enumerate the methods in an object just like you can enumerate the properties. This (or something close to this) should work:

var barfor (bar in foo){    console.log("Foo has property " + bar);}

There are complications to this because some properties of objects aren't enumerable so you won't be able to find every function on the object.


You can use console.dir(object) to write that objects properties to the console.