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

160
Visualizações
Why do extended classes uses prototypes for methods but not for fields?

Looking at this simple code :

class Animal {
    someField = 42;
    animalFunc() {
        console.log('animal')
    }
}
class Lion extends Animal {
    lionFunc() {
        console.loge('lion')
    }
}
let lion = new Lion();
console.log(lion)

Result in Chrome is :

enter image description here

As we can see, the instance methods are on the prototype (for each constructor function)

Question:

Why are fields, as opposed to methods, not on the prototype?
I mean , someField is in Animal not in Lion.

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

0

This is the intended behavior as seen in ECMAScript - 15.7.14 Runtime Semantics: ClassDefinitionEvaluation 25.f:

f. Else if element is a ClassFieldDefinition Record, then
  i. If IsStatic of e is false, append element to instanceFields.
  ii. Else, append element to staticElements.

and in 29.:

Set F.[[Fields]] to instanceFields.

So after we see that this is not a bug, let's see why:

In simple words:

As we know if we have a function on the prototype and we changed that function value, it would be changed to everyone.

If we put the property on the prototype and some instance change the property value it would change the property value in other instances as well and naturally this behavior should not happen so we put the properties on the instance itself.

For static properties this is the intended behavior as we only have one instance of them, this is why they are on the prototype

Extra:

this comment by Ranando D Washington-King from class fields proposal

The quote that I mention is about the problem of add an x data property to the prototype [...]:

Every instance has a copy of the object. The expectation may or may not be exactly that. Consider in Java, the ability to define nested classes. In that case, it is definitely the desire that every instance be able to see the exact same class definition. On the other hand, consider assigning an empty array. The likely intention is for each instance to have its own copy of that array for storing instance-specific values.

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