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

165
Views
how to call the methods of a parent class in a child class object without overriding in child in java script

Let's say I have a parent and a child class as shown below.

class Person { 

  constructor(name) { 
    this.name = name
  }

  greeting() { 
    console.log(`Hi! I'm ${this.name}`); 
  };

}

class Teacher extends Person { 
  constructor(first, subject) { 
    super(first); 

    this.subject = subject; 
  }
}

now lets say I create a global object of the child class

globalThis.teachers = new Teacher("Roby", "Math") 

this global object can directly access the parent class property name. So, I can call,

globalThis.teachers.name \\Will contain Roby

But can I call the method of the parent class, greeting() like this directly without overriding it like the following way?

globalThis.teachers.greeting() \\Will print in console

Snippet:

class Person { 

  constructor(name) { 
    this.name = name
  }

  greeting() { 
    console.log(`Hi! I'm ${this.name}`); 
  };

}

class Teacher extends Person { 
  constructor(first, subject) { 
    super(first); 

    this.subject = subject; 
  }
}

globalThis.teachers = new Teacher("Roby", "Math") 
console.log(globalThis.teachers.name);
globalThis.teachers.greeting();

Is it like every parent class method that I want to access, I will have to override it in the child and from there I will have to call super.greeting().

about 4 years ago · Santiago Trujillo
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!