Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
JavaScript - Confusion between global classes and inheritance

Total begginer who learns JS here. I don't understand why when you declare a variable it doesn't TOTALLY inherit of it's parent class methods, for e.g.:

// I initiate an array (my question is the same for all type of vars)
var myArr = ["foo", "bar"]

// Let's say I call a random function of the parent class Array
console.log(Array.isArray(myArr)); // true

// Since I assume that myArr inherited of the COMPLETE LIST of Array's methods, I should be able to do this:
console.log(myArr.isArray()); // Uncaught TypeError

Why don't the variables inherit of all of the methods of it's parent classes? Instead of that you need to mix between the fonctions of Array and myArr. They should be identitical on the two sides, no?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Array.isArray can also be called on non-arrays, so calling it from the instance of other classes would not have the method, resulting in a runtime error. Basically if you knew it was an array and it would be callable, you would not need to call it.

That is why it is NOT on the Array prototype and NOT callable from the instance.

const a = null
a.isArray() // bad
Array.isArray(a) // good

Developers have the option in Javascript to add methods to the class, the instance (aka prototype), or both. In this case it was only added to the class, not the instance.

It could have been added to the prototype of Object, but then it still would not be on the instances of boolean, number, string, symbol, or undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to pass an array as a parameter to the isArray function.
Eg:

console.log(myArr.isArray(myArr))
about 4 years ago · Juan Pablo Isaza Relatório

0

When you declare a variable it is an instance of a Class, there is not inheritance.

When you declare a class that extends another class is where inheritance occurs.

Array.isArray() is a static property of the JavaScript Array object.

Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda