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

194
Views
How to make property of child class available in constructor of parent?

I'm a bit confused by how inheritance works for class properties. Here's a representative example:

class Base {
    something = 1;
    constructor() { 
        console.log(this.something);
    }
}

class Child extends Base {
    something = 2;
}

new Child();

this logs 1 to the terminal, where I'd expect it to log 2.

This is just a minimal working example. My actual code is more complicated, but basically my requirements are:

  1. Child should have a property,
  2. that property should be accessible in Base.constructor.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't think we can directly access child property from parent class. You can pass value after object creation, but it's not an elegant way.

class Base {
    something = null;
    constructor(something) {
        this.something = something;
        alert(this.something);
    }
}

class Child extends Base{
    something = 2;
}

let childObj = new Child();
let baseObj = new Base(childObj.something);

Parent constructor will always be called when you create child object, so I suggest you to use this.something in a separate method instead of constructor.

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!