Page 1 of 1

Math.func to Number.func

Posted: Sat Nov 08, 2008 3:32 pm
by JellyFish
I'd like to know what you think of this approach to implementing mathematical functions in JavaScript. Rather then Math.pow(base, exponent) wouldn't it be easier, and make more sense, to write (base).pow(exponent)? It is really easy to create such methods.

Code: Select all

 
Number.prototype.pow = function(e) { return Math.pow(this, e); };
 
With this I could write:

Code: Select all

 
var base = 10;
base.pow(6);
(5).pow(2);
 
This feels more object oriented in a way, and it could be done with every math function.

If this was in a library or API of some kind how would you rate this? Is it good or bad? Explain why you think it is what you think it is.

Thanks for reading my brief and arguably uninteresting post. :P

Re: Math.func to Number.func

Posted: Sun Nov 09, 2008 10:01 am
by kaszu
I wouldn't use the library which would do only that since it doesn't add any extra functionality and there are no cross-browser issues with Math.
I use libraries to make my work easier/faster/etc, but library like that just won't make the difference for me.