Use the console to view the available methods for an object?

Can I use the console to view the methods available for a JS object?

I am thinking of something like this:

> var myArray = [1,2,3]; undefined > myArray [1, 2, 3] > myArray.logme = function() { console.log(this); }; function () { console.log(this); } > myArray [1, 2, 3] 

The second time I type myArray , I would like to see that the logme() method is now available.

I want to know the answer to make it easier to explore unfamiliar JS objects.

+6
source share
2 answers

you can use

 console.dir(myArray); 

and you get an extensible / testable screen, for example, custom properties and a prototype object:

(from fooobar.com/questions/939347 / ... , see also What is the difference between console.dir and console.log? ) sub>

+6
source

If you are in Chrome and can use something like the following (rather crude), check if there is a function property:

 function showMethods(obj) { console.log(Object.keys(obj).filter(function(prop) { return typeof el[prop] == 'function'; })); } 

Then just name it like this:

 showMethods({a: 1, b: 2, c: function () {}}) // ['c'] showMethods({a: 1, b: 2}) // [] showMethods({a: 1, b: function() {}, c: function () {}}) // ['b', 'c'] 
+1
source

All Articles