Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

344
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda