Math.func to Number.func

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Math.func to Number.func

Post 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
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Math.func to Number.func

Post 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.
Post Reply