Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:12:40+00:00 2026-06-14T19:12:40+00:00

This question is similar to this this one, except I do not want to

  • 0

This question is similar to this this one, except I do not want to do it in chrome code.

In javascript (running in the browser) I want to keep track of all objects that have been created with a specific construct. Easy, I could do it like this:

var listObjects = [];

function Object() {
  listObjects.push(this);
}

Object.prototype = {
  // class members
}; 

which is fine, except objects are kept even when they are not used anymore (the garbage collector keeps them because in listObjects there is still a reference) creating memory leaks. Now, I could add a “removeObject” function removing an object from the list, but that would require the user to manually call whenever an objects get out of scope.
Now this could be solved if

  1. there would be weak references
  2. one could find out how many references there are to one object
  3. one could define an automatically called destructor

Unfortantly, according to my research, none of these exist in javascript (at least not when it is supposed to run in the browser).

Can anyone think of another way of doing this that works in javascript, or some javascript feature that I missed that could be used to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:12:41+00:00Added an answer on June 14, 2026 at 7:12 pm

    I don’t think there’s any language support for this in JavaScript or that there’s any support for weak-references, at least not in browser environments.

    However, you could attempt something (pretty, pretty, pretty ugly…) keeping your idea of a function Object() override, but having it write to an invisible <div> on the screen the details of the object to track.

    There you go: tracking without holding actual references for objects. But you’d still need to implement a few query functions to retrieve information from that div and clear it over-time, or you’d have a leak as well.

    Or you could do what you suggest: create a general purpose constructor and a general purpose destructor, but that’d require you to call them explicitly to remove objects from your list. But if any unexpected occurs, your destructors won’t get called so you’d probably end up leaking a lot and in unexpected ways.

    Curious to know if others can think of alternatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note this question is similar this one except I'm not working with linq-to-sql, so
This question is similar in concept to this one , except I see I
This question is similar to this one, but with an extra wrinkle: Auto-removing all
The question is similar to this question. However, this one is about exceptions, not
my question is similar to this one , but I don't want to use
This question is similar to Find conditions like 'NOT EXISTS' except that it's working
I have a similar problem to the one enumerated in this question , except
This question is similar to this one How do I add options to a
This question is similar to this other one , with the difference that the
I'm looking for an answer to a question similar to this one: protect users'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.