I want to know how to list all methods available for an object like for example:
alert(show_all_methods(Math));
This should print:
abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random,round, sin, sqrt, tan, …
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
Object.getOwnPropertyNames()to get all properties that belong to an object, whether enumerable or not. For example:You can then use
filter()to obtain only the methods:In ES3 browsers (IE 8 and lower), the properties of built-in objects aren’t enumerable. Objects like
windowanddocumentaren’t built-in, they’re defined by the browser and most likely enumerable by design.From ECMA-262 Edition 3:
I should point out that this means those objects aren’t enumerable properties of the Global object. If you look through the rest of the specification document, you will see most of the built-in properties and methods of these objects have the
{ DontEnum }attribute set on them.Update: a fellow SO user, CMS, brought an IE bug regarding
{ DontEnum }to my attention.In short, beware when naming your object properties. If there is a built-in prototype property or method with the same name then IE will skip over it when using a
for...inloop.