An SO User
An SO User

Reputation: 24998

Understanding Number functions in JavaScript

I was reading about JavaScript Number object on Mozilla Developer Network. I am new to this. Below is my script:

var number = 16;
console.log( Number.prototype.toExponential(number) );
console.log( Number.prototype.toFixed(number) );
console.log( Number.prototype.toPrecision(number) );
// DON'T UNDERSTAND WHAT THIS DOES
// console.log( Number.prototype.toSource(number) );
console.log( Number.prototype.valueOf(number) );  

and the output:

0.0000000000000000e+0 
0.0000000000000000 
0.000000000000000 
0   

I am wondering why I am getting all zeros despite number = 16. Please help me in understanding this. :)

Upvotes: 1

Views: 193

Answers (4)

nanobash
nanobash

Reputation: 5500

You have to have:

var number = 16;
console.log(number.toExponential());
console.log(number.toFixed());
console.log(number.toPrecision());

With prototype you can define you own methods and properties of objects

With prototype basically extending objects

Here is a simple example of prototype:

Number.prototype.isPrime = function() {
    if ( this === 0 || this === 1 ) {
        return false;
    }
    for ( var i = 2; i < this; i++ ) {
        if ( this % i === 0 ) {
            return false;
        }
    }
    return true;
};

var arr = [2,4,5,13,15,121];

for ( var i = 0; i < arr.length; i++ ) {
    console.log(arr[i].isPrime());
}

In this example this keyword refers to number object (so you don't have to pass any argument in function for manipulation)

JSFIDDLE

Take a look on prototype

Upvotes: 3

James M. Lay
James M. Lay

Reputation: 2470

You will have better luck if you do the following:

var number = new Number(16);
console.log(number.toExponential());
console.log(number.toFixed());
console.log(number.toPrecision());
console.log(number.valueOf());

Note* It's preferable not to use numbers in javascript this way unless you have a specific need to avoid primitives.

The reason you are getting 0 is because the Number prototype defaults to zero if it is not instantiated.

Number is a prototype which is designed semantically to be used to spawn new objects which inherit these methods. These methods are not designed to be called directly from the Number prototype, but instead from the numbers themselves. Try the following:

(16).toExponential();

You have to wrap the number in parenthesis so the interpreter knows you're accessing methods and not defining a floating point.

The important thing here to understand is that the Number prototype provides all the methods that all numbers will inherit in javascript.

To explain further why you are getting a 0, the methods on the Number prototype are intended to be "bound" to a number object. They will use the bound object for input and will ignore any arguments. Since they are bound to the default Number object, the default number is 0, and therefore all methods will return their version of 0.

There is a way in javascript to rebind methods to an object using the "call" method (there's also bind and apply):

Number.prototype.toExponential.call(16);

Upvotes: 2

phylax
phylax

Reputation: 2086

try this:

var number = 16;
console.log( Number.prototype.toExponential.call(number) );

Notice the call() which calles the method toExponential of the Number object on an instance.

Upvotes: 1

Number.prototype functions live on actual numbers (the prototype is the definition for a specific type. String variables have functions that live on String.prototype, arrays have functions defined on Array.prototype, etc. http://javascript.crockford.com/prototypal.html is a good starting place to learn about prototype inheritance), so just call number.toFixed(), number.toFixed() etc. You can even do (16).toString() etc.

You're calling the function "on nothing" by calling the prototype function, instead of "on 16".

Upvotes: 2

Related Questions