Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Number.prototype a Number

({}).toString.call(Number.prototype) === "[object Number]"

The Number prototype object is itself a Number object (its [[Class]] is "Number") whose value is +0.

15.7.4

Why would it be useful for Number.prototype to be a Number? (the same goes for every other built-in prototype which has the [[Class]] set to not Object)

I'm picking on Number.prototype specifically because I can imagine sensible legacy reasons for Array.prototype and Date.prototype.

like image 363
Raynos Avatar asked Apr 30 '12 22:04

Raynos


People also ask

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.

What is number () in JavaScript?

Javascript Number() Function object: This parameter holds the objects that will be converted any type of javascript variable to number type. Return Values: Number() function returns the number format for any type of javascript variable. Example 2: Not a number is returned by the compiler.

How do you Stringify a number?

The toString() method in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object. The toString() method is used with a number num as shown in the above syntax using the '.

Is number an object in JavaScript?

Number is a primitive wrapper object used to represent and manipulate numbers like 37 or -9.25 . The Number constructor contains constants and methods for working with numbers.


2 Answers

In general, Constructor.prototype is an exemplar of the "type" defined by Constructor. Although things seem to get hairy for immutable primitives, and especially once you involve the boxing stuff, this exemplar concept still makes sense, with 0 being the "exemplar" of Number.

like image 52
Domenic Avatar answered Sep 30 '22 16:09

Domenic


The Number prototype object is itself a Number object (its [[Class]] is "Number") whose value is +0

Why wouldn't Number.prototype be a Number object? Its [[Prototype]] is Object.prototype, so it still inherits from Object.

like image 37
RobG Avatar answered Sep 30 '22 16:09

RobG