Number
would be a function or object?
TL;DR: It is a constructor function (and therefore an object too, learn more in the documentation and in the question Why Arrays and Functions are Objects?). When called, it returns a numeric primitive and, when instantiated, returns an object ("instance") Number
. It also has methods and properties of its own - "static" - that will not be passed on to primitives and built objects (such as isInteger
) and methods and properties in their prototype that will be passed on to primitive and constructed objects (such as toFixed
).
Brief history of the primitives
It is incorrect to say that, in Javascript, everything is a function. Probably there was a mistake, since it is common to say that everything in Javascript is an object.
But not even that last statement is true, since the primitive are not, in fact, objects. All other values are objects. The primitives are undefined
, null
, boolean
, number
, bigint
, string
and symbol
.
There are some differences, such as the fact that primitives are passed by value and objects by reference. But there is, between primitive and objects, a remarkable similarity: both may possess estates. This creates confusion, as some people may (erroneously!) think that because they have properties, they are both objects.
It is worth noting that the estates (and methods) of the primitives come from constructing functions associated with the primitive type. Thus:
- Primitives of the type
number
associate with the manufacturer Number
;
- Primitives of the type
string
associate with the manufacturer String
;
- And the same for
boolean
(Boolean
), bigint
(BigInt
) and symbol
(Symbol
).
These associations give the primitives their properties and methods. As, for example, the method toFixed
, that the builder Number
offers all the primitives of the type number
. Or the property length
that string
s possess. This subject is a little more advanced and, to better understand, seek to study about the Javascript prototype chain.
Only the primitive null
and undefined
nay have this association and therefore also have no property and no method.
And builders, like Number
?
With the brief explanation above, we can conclude that although number
be a primitive, Number
is the builder associated with all type primitives number
. Thus, it is said that Number
is, in fact, a constructor function - in this case, associated with the primitive number
.
Note, in the section above, that I did not speak at any time of functions such as primitive or objects. This is because, in a way, they are neither primitive nor objects. A documentation classifies functions and objects as "structural types" of the language.
Remember that, like objects, functions also have properties, methods and are amenable to modifications. In contrast, primitives may have properties, methods, but are immutable.
In a way, Number
(and the other construction functions) allow the construction of their associated primitive when applied (invoked):
// Aplicação da função construtora:
const myConstructedNumberPrimitive = Number('123');
console.log(
myConstructedNumberPrimitive,
typeof myConstructedNumberPrimitive
); // 123 number
// Utilização da função construtora para instanciação de um objeto:
// ↓↓↓
const myInstantiatedNumberObject = new Number('123');
console.log(
myInstantiatedNumberObject,
typeof myInstantiatedNumberObject
); // Number {123} object
On the console of snippet from Stackoverflow, an "empty object appears" ({}
). Use the browser console to inspect that it is, of course, an instantiated object from the manufacturer Number
.
The use of new
is explained more subtly here.
In short:
Number
is a construction function that allows the creation of primitive, if applied; or allows the instantiation of objects, if the operator new
used. These two cases were demonstrated above.
Like primitives and objects, functions can also possess properties or methods. So, isInteger
is a method of Number
.
Note that isInteger
, as a method of Number
, will not be available as primitive method number
(returned by the constructor’s application) or objects (returned by the constructor’s instantiation). The "past" down methods must be in the prototype of Number
.
See the difference:
console.log(typeof Number.isInteger); // (function) Disponível. Propriedade do construtor `Number`.
console.log(typeof Number.toFixed); // (undefined) Indisponível. Propriedade do protótipo.
console.log(typeof (1).isInteger); // (undefined). Indisponível. É propriedade do construtor, mas não do primitivo.
console.log(typeof (1).toFixed); // (function). Disponível. É propriedade, trazida do protótipo do construtor.
// Mostra os métodos e propriedades da função construtora `Number`.
// Estes métodos e propriedades não serão passados ao primitivo e
// objetos construídos por este construtor.
console.log(
'Propriedades do construtor:',
Object.getOwnPropertyNames(Number)
);
// Mostra os métodos e propriedades do protótipo da função construtora `Number`.
// São estes métodos e propriedades que serão passados ao primitivo e
// objetos construídos por este construtor.
console.log(
'Propriedades do protótipo do construtor:',
Object.getOwnPropertyNames(Number.prototype)
);
Read the Manual on how NOT to ask questions and also read What a mistake I made asking my question?
– Solkarped
I removed the second question about "encapsulated global object" because the idea of the site is to focus each question on a specific subject. If you want, you can open another question about it (not forgetting to search first on the site if there is already a question on the subject)
– hkotsubo