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

342
Views
Javascript - Do references to methods prevent objects from being garbage collected?

Suppose I have function that returns a method of an object that only exists in stack scope:

class Item {
  constructor(name) { this.name = name }
  print() { console.log(`Item with name: ${this.name}`); }
}

function namePrinter(name) {
  return (new Foo(name)).bar
}

const printer = namePrinter('Alan')
printer() // prints 'Alan'

Does the reference to the print() method prevent the Foo instance from being garbage collected? My assumption is that underneath the ES6 OO sugar the bar() method gets bound to the Foo instance, causing it to store a reference to the object so that it can substitute it into this, but I'm not sure.

Side note - I realize I could make this work with something like the following, where the instance is part of the closure to an anonymous function, but that wouldn't work for my particular use case:

function newPrinter(name) {
  const item = new Item(name)
  return () => {
    item.print()
  }
}
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!