10

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?

3
  • Just curious, why do you need it? Commented Sep 25, 2011 at 20:22
  • mymfunc shouldn't appear in the enumeration because you're enumerating a different object (Math vs Object.prototype). If you're running that code in the console, it may appear as though it does, but really the console is just displaying the result of the assignment. Commented Sep 25, 2011 at 20:31
  • One possible use - documentation tool for JS built in objects. Another use - building a UI to the Math object (a calculator eg) Commented Sep 25, 2011 at 20:44

5 Answers 5

15

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

6
var functionNames = [];

Object.getOwnPropertyNames(obj).forEach(function(property) {
  if(typeof obj[property] === 'function') {
    functionNames.push(property);
  }
});

console.log(functionNames);

That gives you an array of the names of the properties that are functions. Accepted answer gave you names of all the properties.

Comments

2

The specification doesn't appear to define with what properties the Math functions are defined with. Most implementations, it seems, apply DontEnum to these functions, which mean they won't show up in the object when iterated through with a for(i in Math) loop.

May I ask what you need to do this for? There aren't many functions, so it may be best to simply define them yourself in an array:

var methods = ['abs', 'max', 'min', ...etc.];

Comments

0

console.log(Math) should work.

1 Comment

Except that doesn't give you programmatic access to them, which I imagine is what the OP is looking for.
0

Object.getOwnPropertyNames() is a good solution


The following example is if you have written a sample script and want to create a button for each function.

<!-- <script src="example.js"></script> -->

<script>
// example.js
function Example_foo() {
  console.log("foo")
}

function Example_bar() {
  console.log("bar")
}

for (const funcName of Object.getOwnPropertyNames(this)) {
  if (funcName.startsWith("Example")) {
    const frag = document.createRange().createContextualFragment(`
      <button onclick="${funcName}()">${funcName}</button><br>
    `)
    document.body.append(frag)
  }
}
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.