Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!