Doubt about Number.isInteger() in JS. Number is an object or function?

Asked

Viewed 186 times

4

Number would be a function or object?

My teacher said that everything in Javascript can be seen as a function (even objects) and it confused me a little. He told me that Number would be an encapsulated global object, but to have an object it is necessary for it to be instantiated from a class, so how is this possible?

He just played the following code: console.log(Number.isInteger(variavelX)) directly without instantiating the Number. So I thought that Number was an object and that isInteger() was a method of his class, but he did not even urge such an object. So, how can I understand this concept?

2 answers

9


Number is an object or function?

Both.


Technically speaking, Number is a function:

console.log(typeof Number); // function

But it’s a "construction function", which allows instances to be created through the operator new.

Only that functions are also objects, and any object can have "coupled" functions to it:

// um objeto qualquer
let obj = { id: 1, nome: 'Fulano' };
// definir uma propriedade que é uma função
obj.funcao = function() { console.log('oi') };
// chamar a função
obj.funcao(); // oi

Therefore, nothing prevents doing the same with a function:

function Abc() {
    // função faz algo...
}

Abc.fazOutraCoisa = function(n) {
    console.log(n);
}
Abc.fazOutraCoisa(20); // 20

Note that I don’t need to create an instance of Abc to use the function fazOutraCoisa. For it is an attribute of function itself Abc. In the same way that isInteger is a function of Number (of the function itself Number, not from your instances, so you don’t need to instantiate anything to use it).

In case the function is constructor, it is still a way to simulate the static methods of other languages. So much so that isInteger is listed on section of "static methods" of Number.


"But I thought it was a class"

Deep down, Javascript has no classes. Even though the current version of the language supports the class, it is only a syntactic sugar to create the constructor function:

class Abc {
    constructor(n) {
        this.valor = n;
    }

    metodo() {
        console.log(this.valor);
    }

    static metodoEstatico() {
        console.log(this.valor); // undefined, pois a função/classe Abc não tem o atributo "valor"
    }
}

// Abc é uma função!
console.log(typeof Abc); // function

let a = new Abc(10);
a.metodo(); // 10
Abc.metodoEstatico(); // undefined

The above code is equivalent to this:

function Abc(n){
    this.valor = n;

    this.metodo = function() {
        console.log(this.valor);
    }
}

Abc.metodoEstatico = function() {
    console.log(this.valor); // undefined, pois a função/classe Abc não tem o atributo "valor"
}

console.log(typeof Abc); // function
let a = new Abc(10);
a.metodo(); // 10
Abc.metodoEstatico(); // undefined

  • Thank you so much for the explanation! Sanou all my doubts and for sure it was for my notes :)

6

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 strings 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)
);

  • Thank you for supplementing the answer of the above colleague! It was of utmost importance and is already in my notes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.