For some reason for(var i in Math){console.log(i)} doesn't show the expected tan, cos, atan2, E, PI in Javascript.
Because Math
is a built in object whose properties are flagged non-enumerable. Many built in objects have this behavior, which is why looping over an array with for..in
will not give you problems until Array.prototype
is extended with user functions, which are always enumerable by default.
Until recently non-enumerable was an internal property not accessible by regular Javascript code. However EMCAScript 5 specifies the ability to set the enumerability and writeability (try changing the value of Math.PI
) of any object property through Object.defineProperty().
It also provides Object.getOwnPropertyNames() as a way to get a list of all properties of an object regardless of their enumerability.
Object.getOwnPropertyNames(Math);
//returns
["LN10", "PI", "E", "LOG10E", "SQRT2", "LOG2E", "SQRT1_2", "LN2", "cos", "pow", "log", "tan", "sqrt", "ceil", "asin", "abs", "max", "exp", "atan2", "random", "round", "floor", "acos", "atan", "min", "sin"]
Far as I know the only browsers that currently support these functions are Chrome and Safari. Firefox should support it at version 4. IE9 I am not sure about, but Microsoft has stated they intend to support the EMCAScript 5 standard eventually.
I do not believe there is any way to emulate this functionality in Javascript interpreters without explicit support.
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