Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda