I want to make a function that automatically turns every “Math” function global
example:
floor = Math.floor
This allows me to do this:
alert(floor(2.5))
The example above works perfectly (it alerts “2”), but I can’t seem to find a way to automate this process for every function in the “Math” object. I looked on the internet and I tried to make something myself but… nothing works.
answer:
(function(a,b){for(b in a=Object.getOwnPropertyNames(Math))this[a[b]]=Math[a[b]]})()
Actually you might think you could just loop over all the properties from the
Mathobject and turn any function into a global one (by copying its reference):This would work.. if the word would wouldn’t be here 🙂
We can discover the problem with
Object.getOwnPropertyDescriptor:..and as we can see, all
methodsare set toenmuerablefalse.edit
I actually forgot about
Object.getOwnPropertyNames. You indeed could get all property names with that method, like