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

187
Views
Why does a class have a "constructor" field in JavaScript?

A class is supposed to have a prototype field that supplies default fields. For example, prototype.constructor defaults to the constructor function (i.e., the class itself) and is therefore accessible as the constructor field in new objects:

function MyClass(){}
console.log(MyClass.prototype.constructor === MyClass); // true
console.log(new MyClass().constructor === MyClass); // true

That's all fine and good. However, if we accidently confuse the class and the object, we would expect an error, since the class shouldn't have a constructor field. But it does:

console.log("constructor" in MyClass);        // true
console.log(MyClass.constructor === MyClass); // false

Why does the class itself have a constructor field, and why is it not the same as protoype.constructor? Wouldn't this lead to lots of bugs, since a program could inadvertently access the constructor field of a class?

about 4 years ago · Juan Pablo Isaza
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!