Correct way to reset or clear a Javascript object?

I have a Javascript class that contains several 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 as follows:

// 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 because of the limitation that I want to reuse the same object utilsin all the code without the need to reinitialize it. In addition, I want the ParamHash object to be recreated from scratch every time I call it. However, subsequent calls to the ParamHash constructor cause the error "Object does not support this action." At this point, I see that the utils.paramHash object still contains the old values ​​("a", "b").

ParamHash, , . , ( doSomething()):

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

, , -... reset ? , : reset ParamHash ? , / . - :

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

: - - , IE6 + FF 2 +.


: Cristoph , / MyUtils, ParamHash.

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();
+3
3

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

, ParamHash() .

utils.ParamHash.constructor

, .


, , , . , , . - :

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();

hasOwnProperty() , , Object.prototype.


:

  • JavaScript, .
  • items , , .. this.items = {};
+3

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

ParamHash . new ParamHash() , utils.ParamHash .

:

var utils = new MyUtils();
utils.paramHashInstance = new utils.ParamHash("a", 1, "b", 2);
utils.DoSomething();
+2

Have you tried to exclude a new keyword?

utils.ParamHash = utils.ParamHash("c", 3);
0
source

Source: https://habr.com/ru/post/1703911/


All Articles