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?
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.
The Object. getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object.
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.
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]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With