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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:32:27+00:00 2026-05-11T06:32:27+00:00

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

  • 0

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(); 
  • 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. 2026-05-11T06:32:28+00:00Added an answer on May 11, 2026 at 6:32 am

    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 = {};
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Enterprise Library Logging Application Block. May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer You may want to look at this page. It was… May 12, 2026 at 12:41 am
  • Editorial Team
    Editorial Team added an answer I solved this problem by creating a separate console application… May 12, 2026 at 12:40 am

Related Questions

I'm considering porting a very simple text-templating library to scala, mostly as an exercise
Hopefully this example will illustrate my point better than simply trying to explain it:
Situation: I have a simple XML document that contains image information. I need to
I'm building a basic forum where every post contains some text, first and last

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.