Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to reset or clear a Javascript object?

I have a Javascript class that contains a few functions and member objects:

function MyUtils()
{
  // Member Variables (Constructor)
  var x = getComplexData();
  var y = doSomeInitialization();

  // Objects
  this.ParamHash = function()
  {
    // Member variables
    this.length = 0;
    this.items = new Array();

    // Constructor
    for (var i = 0; i < arguments.length; i += 2)
    {
      // Fill the items array.
      this.items[arguments[i]] = arguments[i+1];
      this.length++;
    }
  }

  // Functions
  this.doSomething = function()
  {
    // Do something.
    // Uses the items in the ParamHash object.
    for (var i in this.ParamHash.items)
    {
      // Really do something!
    }

    // Clear the ParamHash object -- How??
  }
}

This is invoked in the following manner:

// First call - works fine.
var utils = new MyUtils();
utils.paramHash = new utils.ParamHash("a", 1, "b", 2);
utils.doSomething();

// Don't want to re-initialize.
// utils = new MyUtils();

// Consequent call - crashes ["Object doesn't support this action."].
utils.paramHash = new utils.ParamHash("c", 3);
utils.doSomething();

The problem arises from the restriction that I want to reuse the same utils object throughout the code without having to re-initialize it. Also, I want the ParamHash object to be recreated from scratch everytime I call it. However, consequent calls to the ParamHash constructor throw an error "Object doesn't support this action." At this stage, I can see that the utils.paramHash object still contains the old values ("a", "b").

I have tried various ways to clear the ParamHash object such as setting it's items and length to null, popping items from the array. Nothing seemed to work until I used the following way (in the doSomething() function):

this.paramHash.items = new Array();
this.paramHash.length = 0;

This seems wrong because what if I had a lot of member variables... would I have to reset each of them individually? So, the question is: What is the best way to reset the ParamHash object to the initial state? I'm sure hoping that there is a cleaner/more direct way. Something like :

// Doesn't work! :-(
this.paramHash = new function() {};

EDIT: I'm looking for a cross-browser solution - One that works atleast in IE6+ and FF 2+.


Solution: Thanks to Cristoph, I was able to do it by creating a separate variable/property within MyUtils which only holds the instance of the ParamHash function.

function MyUtils()
{
  // Same ol' stuff.
  var myParamHash;
}

// First call - works fine.
var utils = new MyUtils();
utils.myParamHash = new utils.ParamHash("a", 1, "b", 2);
utils.doSomething();

// Consequent call - works fine now.
utils.myParamHash = new utils.ParamHash("c", 3);
utils.doSomething();
like image 990
Cerebrus Avatar asked May 22 '26 06:05

Cerebrus


1 Answers

This

utils.ParamHash = new utils.ParamHash("a", 1, "b", 2);

overwrites the the property which holds the ParamHash() constructor function with an instance object. You could get the constructor back via

utils.ParamHash.constructor

but the cleaner way would be to not overwrite it in the first place and use a seperate property to hold the instance.


I don't know the exact problem Cerebrus is trying to solve, so there might be valid reasons for what he's doing. But in my opinion, his solution is overly complicated. I'd do something like this:

function MyUtils() {
    this.x = getComplexData();
    this.y = doSomeInitialization();
    this.params = {};
}

MyUtils.prototype.doSomething = function() {
    for(var prop in this.params) {
        if(this.params.hasOwnProperty(prop)) {
            // do stuff
        }
    }
};

var utils = new MyUtils;
utils.params = { a : 1, b : 2 };
utils.doSomething();

The check for hasOwnProperty() is unnecessary if you can be sure that no one messed with Object.prototype.


Some additional comments:

  • in JavaScript, normally only the names of constructor functions are capitalized
  • items shouldn't be an array, but a plain object, ie this.items = {};
like image 195
Christoph Avatar answered May 24 '26 20:05

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!