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

166
Views
Is Javascript's class method instantiated?

For example, if I have a class ImageBlock and then that class has a complex method modifyImage()...

class ImageBlock {
  /* ... */

  modifyImage() {
    // A 40+ line code, complex code
  }  

  /* ... */
}

When I create a new instance of ImageBlock, will modifyImage() will be instantiated too? as another function?

I don't want have two instance of complex method, as it was memory waste. I know another alternative for this, but I don't ask for alternative solution to this, sorry.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

as another function?

No, it'll be the exact same function.

class ImageBlock {
  /* ... */

  modifyImage() {
    // A 40+ line code, complex code
  }  

  /* ... */
}

const i1 = new ImageBlock();
const i2 = new ImageBlock();
console.log(
  i1.modifyImage === i2.modifyImage
);

Methods defined inside a class are put onto the prototype object, once, and then each instance has an internal prototype of that prototype object.

That said, even if it was created anew each time (such as with a class field arrow function in order to preserve reference to this inside - which is reasonably common) -

as it was memory waste.

A 40+ line code

it would be absolutely nothing to worry about in 99.9% of situations. Computers from the past decade can run many thousands of lines in the blink of an eye, and simply creating a function without calling it is much easier than that.

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!