Augmenting Number.prototype with the functions from Math

Xavier MONTILLET xavierm02.net at gmail.com
Wed Jan 25 10:32:49 PST 2012


Hi,

I think it'd be nice to have the functions available in Math in
Number.prototype.
e.g.

Number.prototype.pow = function ( p ) {
    return Math.pow( this, p );
};

Since pow is supposed to be an operator, I feel better when writing
a.pow( b ) than Math.pow( a, b );

About the other methods, I'm not sure. They really are "functions" in
maths so it doesn't feel that weird calling them with Math.f( ).
Moreover if you store them in local variables.
But I still find doing a.abs( ).ceil( ) is way more convenient than
Math.ceil( Math.abs( a ) ).

So since numbers are litterals and therefore extending the prototype
won't break anything, why not add it?


More information about the es-discuss mailing list