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 8926131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:53:36+00:00 2026-06-15T07:53:36+00:00

When defining a class as such: var namespace = {}; namespace.appleClass = function() {

  • 0

When defining a class as such:

var namespace = {};
namespace.appleClass = function() {
   this.collection = [];
};

namespace.appleClass.prototype.methodOne = function(arg) {
   this.collection.push(arg);
};

The way to get rid of this.collection is simple.

this.collection.length = 0;

My question is what happens to the methods defined above (on namespace.appleClass.prototype). How does JavaScript deal with them? Where is a function ‘stored’? How can that storage be freed up?

The above class can be used as such.

var apple = new appleClass();
appleClass.methodOne(2);
appleClass.collection[0]// will be equal to two etc..

I know how to null DOM references, COM references and the long story behind the delete operator. What I don’t know is what is the best way to delete functions and references to them from a ‘custom prototype’ or ‘class’ to say so.

  • 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-15T07:53:38+00:00Added an answer on June 15, 2026 at 7:53 am

    The way to get rid of this.collection is simple.

    this.collection.length = 0;
    

    That doesn’t get rid of the array, that just truncates it so it has no entries. To actually get rid of the array entirely, do delete this.collection (which will remove the property referring to the array entirely, making the array eligible for garbage collection) or this.collection = null (which will release the reference to the array, making it eligible for garbage collection, but not remove the property, which will still exist with the value null).

    Anything that was in this.collection and which isn’t referred to elsewhere will then be eligible for garbage collection. When (or indeed whether) this happens is entirely down to the JavaScript implementation.

    And that’s the key point, really: To "manage" memory in JavaScript, you just make sure nothing is referring to the thing you don’t want anymore. Usually that falls out of your application logic anyway (you don’t want it anymore, so you drop your references to it). If you design your code keeping in mind how closures work, for the most part, you don’t need to worry about memory management.

    Re your questions in the comments on the question:

    I am taking about the methods found under namespace.appleClass.prototype. How does JavaScript deal with them? Where is a function ‘stored’? How can that storage be freed up?

    The function object is stored in heap (almost certainly; if it were only referenced by a local variable, some engines like Google’s V8 might well use stack for it); where the code is is implementation-dependent. A reference to the function object (and therefore to its code) is stored in the property on the prototype object on appleClass. There is only one of these functions, which is shared by all instances created by new namespace.appleClass via the prototype chain. There is not a separate copy of the function for each instance (just a separate reference to it), and thus there is no need to worry about "cleaning up" that function on instances.

    If you no longer need that function at all, anywhere in your code (e.g., all appleClass instances no longer need it), you can get rid of it via delete namespace.appleClass.prototype.methodOne). That will remove the property from the prototype, which releases its reference to the function object, which releases its reference to the function code, all of which are then eligible for garbage collection. But this would be very unusual, and a single function doesn’t take up much memory in any case.

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

Sidebar

Related Questions

Which is the preferred way of defining class properties in Python and why? Is
Possible Duplicate: Two css files defining same class The answers to this question and
A fairly basic problem for a change. Given a class such as this: public
When defining a method on a class in Python, it looks something like this:
Given the following two ways of defining a class method in Ruby: class Foo
In Apple's The Objective-C Programming Language: Defining a Class the section named Redefining self
I am defining a Form class which has a filefield and is not mandatory
For my tests I'm using a base class MyTestBase defining a method setup() that
What is a simple example of decorating a class by defining the decorator as
After defining the following : abstract class A { type T def print(p: T)

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.