Is there a way to print all methods of an object in JavaScript?
6 Answers
Sure:
function getMethods(obj) {
var result = [];
for (var id in obj) {
try {
if (typeof(obj[id]) == "function") {
result.push(id + ": " + obj[id].toString());
}
} catch (err) {
result.push(id + ": inaccessible");
}
}
return result;
}
Using it:
alert(getMethods(document).join("\n"));

- 115,121
- 27
- 131
- 155
-
2the try/catch is a good approach. There are some property/methods in IE that will error out on access. – scunliffe Sep 30 '08 at 12:26
-
Yeah, I think there are some in Firefox as well. – troelskn Sep 30 '08 at 14:03
-
3Note that it doesn't work on some built-in objects like Date: `var a = new Date(); console.log(typeof a, getMethods(a));` Returns: `object []`. – FGM May 24 '16 at 15:02
-
For `Date`, this worked for me : `Object.getOwnPropertyNames(Date.prototype)` – Aakash Oct 14 '22 at 07:21
If you just want to look what is inside an object, you can print all object's keys. Some of them can be variables, some - methods.
The method is not very accurate, however it's really quick:
console.log(Object.keys(obj));

- 4,674
- 2
- 34
- 36
-
7
-
@old-geezer This returns all date methods, `Object.getOwnPropertyNames(Date.prototype).join("\n")` – vikkee Mar 28 '22 at 20:36
Here is an ES6
sample.
// Get the Object's methods names:
function getMethodsNames(obj = this) {
return Object.keys(obj)
.filter((key) => typeof obj[key] === 'function');
}
// Get the Object's methods (functions):
function getMethods(obj = this) {
return Object.keys(obj)
.filter((key) => typeof obj[key] === 'function')
.map((key) => obj[key]);
}
obj = this
is an ES6 default parameter, you can pass in an Object or it will default to this
.
Object.keys
returns an Array of the Object
's own enumerable properties.
Over the window
Object it will return [..., 'localStorage', ...'location']
.
(param) => ...
is an ES6 arrow function, it's a shorthand for
function(param) {
return ...
}
with an implicit return.
Array.filter
creates a new array with all elements that pass the test (typeof obj[key] === 'function'
).
Array.map
creates a new array with the results of calling a provided function on every element in this array (return obj[key]
).

- 4,356
- 3
- 29
- 51

- 500
- 6
- 5
-
2This is useful! One correction: inside the functions `obj` should be used instead of `this`. – thecodesmith_ Jun 26 '17 at 21:31
Take a gander at this code:-
function writeLn(s)
{
//your code to write a line to stdout
WScript.Echo(s)
}
function Base() {}
Base.prototype.methodA = function() {}
Base.prototype.attribA = "hello"
var derived = new Base()
derived.methodB = function() {}
derived.attribB = "world";
function getMethods(obj)
{
var retVal = {}
for (var candidate in obj)
{
if (typeof(obj[candidate]) == "function")
retVal[candidate] = {func: obj[candidate], inherited: !obj.hasOwnProperty(candidate)}
}
return retVal
}
var result = getMethods(derived)
for (var name in result)
{
writeLn(name + " is " + (result[name].inherited ? "" : "not") + " inherited")
}
The getMethod function returns the set of methods along with whether the method is one that has been inherited from a prototype.
Note that if you intend to use this on objects that are supplied from the context such as browser/DOM object then it won't work IE.

- 187,081
- 35
- 232
- 306
From here:
Example 1: This example writes out all the properties of the "navigator" object, plus their values:
for (var myprop in navigator){
document.write(myprop+": "+navigator[myprop]+"<br>")
}
Just replace 'navigator' with whatever object you are interested in and you should be good to go.
As mentioned by Anthony in the comments section - This returns all attributes not just methods as the question asked for.
Oops! That'll teach me to try and answer a question in a language I don't know. Still, I think the code is useful - just not what was required.

- 4,537
- 5
- 34
- 34
-
This returns all attributes not just methods as the question asked for. On IE it only returns some of the properties and none of the methods. – AnthonyWJones Sep 30 '08 at 10:50
Since methods in JavaScript are just properties that are functions, the for..in loop will enumerate them with an exception - it won't enumerate built-in methods. As far as I know, there is no way to enumerate built-in methods. And you can't declare your own methods or properties on an object that aren't enumerable this way.

- 26,428
- 5
- 49
- 48