Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all functions of an object in JavaScript

For example,

Math.mymfunc = function (x) {   
    return x+1;
}

will be treated as a property and when I write

for(var p in Math.__proto__) console.log(p)

it will be shown. But the rest of Math functions will not. How can I get all functions of a Math object?

like image 982
Andr Avatar asked Sep 25 '11 20:09

Andr


People also ask

How do you find the function of all objects?

We can use the Object. getOwnPropertyNames() function to get all the property names linked to an object. Then we can filter the resulting array, to only include that property name if it's a function. We determine if it's a function by using typeof on it.

Which lists all methods defined by an object?

The Object. getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.

How can you get the list of all properties in an object in JavaScript?

getOwnPropertyNames() The Object. getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.


1 Answers

Object.getOwnPropertyNames(Math); is what you are after.

This logs all of the properties provided you are dealing with an EcmaScript 5 compliant browser.

var objs = Object.getOwnPropertyNames(Math);
for(var i in objs ){
  console.log(objs[i]);
}
like image 55
Lime Avatar answered Oct 30 '22 21:10

Lime