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

157
Views
In a javascript Class declaration, which is the diference between declaring a method inside the constructor and the body?

I need some light here please! I understand that everything that is inside the constructor function its called with the new keyword and initialized. so as shown in the snippet, below when I log the mycar instance, i can see the method declared inside the constructor but not the one declared in the body.

so the question are: 1)why im able to call succesfully the method defined in the body if is not among the propertys of the instance? 2) why at all I declare the methods in the body and not just inside the constructor? in the snipet below both are working

class car {
  constructor(_brand, _year, _color) {
    this.brand = _brand;
    this.year = _year;
    this.color = _color;
    // a method defined inside the constructor--------------------------
    this.get_brand_color = function () {
      return this.brand + this.color;
    };
  }
   // a method defined in the body of the class-----------------------------
  get_brand_year() {
    return this.brand + this.year;
  }
}

mycar = new car("chevrolet ", 1997, "red");

console.log(mycar); 
// logs: car {brand: 'chevrolet ', year: 1997, color: 'red', get_brand_color: ƒ}
console.log(mycar.get_brand_year());
// logs:chevrolet 1997
console.log(mycar.get_brand_color());
// logs:chevrolet red

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!