I have a Javascript class that contains several functions and member objects:
function MyUtils()
{
var x = getComplexData();
var y = doSomeInitialization();
this.ParamHash = function()
{
this.length = 0;
this.items = new Array();
for (var i = 0; i < arguments.length; i += 2)
{
this.items[arguments[i]] = arguments[i+1];
this.length++;
}
}
this.doSomething = function()
{
for (var i in this.ParamHash.items)
{
}
}
}
This is invoked as follows:
var utils = new MyUtils();
utils.paramHash = new utils.ParamHash("a", 1, "b", 2);
utils.doSomething();
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 ? , / . - :
this.paramHash = new function() {};
: - - , IE6 + FF 2 +.
: Cristoph , / MyUtils, ParamHash.
function MyUtils()
{
var myParamHash;
}
var utils = new MyUtils();
utils.myParamHash = new utils.ParamHash("a", 1, "b", 2);
utils.doSomething();
utils.myParamHash = new utils.ParamHash("c", 3);
utils.doSomething();