I know this is possible in python, but can i get a list of methods for a javascript object?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
This function receives an arbitrary object and returns the name of it's prototype, a list with all it's methods and an object with the name of it's properties (and their types). I haven't the opportunity of testing it in a browser, but it works with Nodejs (v0.10.24).
Example (with Nodejs):
Output:
The following examples also work with Nodejs:
You can loop over the properties in the object and test their type.
To add to the existing answers, ECMAScript 5th ed. provides a way to access all properties of an object (even the non-enumerable ones) using the method
Object.getOwnPropertyNames
. When trying to enumerate the properties of native objects such asMath
, afor..in
will print nothing on the console. However,
will return:
You could write a helper function on top of this that only returns methods given an object.
Support for ECMAScript 5th ed. is somewhat bleak at this point, as only Chrome, IE9pre3, and Safari/Firefox nightlies support it.
A single-line solution