Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Number.prototype in javascript and the Math object?

I've always wondered why Javascript has the global Math object instead of giving numbers their own methods. Is there a good reason for it?

Also are there any drawbacks (other than efficiency) to doing something like this?:

Number.prototype.round = function(){     return Math.round(this); }; 

Just to make clear, I understand that constants like π need somewhere and functions that are applied on more than one number like min/max. The question was mainly concerning methods which only effect a single number such as round, abs, sin, pow, etc.

like image 546
AnnanFay Avatar asked Mar 07 '09 19:03

AnnanFay


People also ask

What is prototype object in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

What is number prototype?

Definition and Usage prototype allows you to add new properties and methods to numbers. prototype is a property available with all JavaScript objects.

Why is extending built-in JavaScript objects not a good idea?

Extending the JavaScript built-in object is not a good idea because if browser/JS has decided that they will provide the same method that you have extended, then your method will be override and the JS implementation (which may be difference from yours) would take over.

What is prototype in JavaScript with example?

In JavaScript, every function and object has a property named prototype by default. For example, function Person () { this.name = 'John', this. age = 23 } const person = new Person(); // checking the prototype value console.


1 Answers

The reason for the Math object is simple: "because Java does it". Not the best of reasons, but here we are. I guess things made more sense back then, before Douglas Crockford started his campaign to suppress half the language*. Originally you were "allowed", or meant, to do things like this:

with (Math) {   var n = min( round(a) * round(b), sqrt(c) );   var result = exp( n + d ); } 

The drawback to extending Number.prototype is that someone else might do the same thing. Or worse, for example, define Number.prototype.round as a symmetrical rounding function.

If you are looking for ways to make your life easier, why stop there? Why not simply include Math functions as global functions?

var m = 'abs acos asin atan atan2 ceil cos exp floor log max min ' +         'pow random round sin sqrt tan PI').split(' '); for (var i=0,l=m.length; i<l; i++) {   window[ m[i] ] = Math[ m[i] ]; } 

This will drop all the math functions into the global scope, effectively allowing you to stop typing "Math." Ask yourself: Is there any real difference between extending Number and extending window with these functions?

* Before you flame me: The Crockford comment is not meant to be taken too seriously. I do agree with him that with is very dangerous in an implicit global environment.

like image 153
Borgar Avatar answered Oct 05 '22 20:10

Borgar