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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:59:44+00:00 2026-05-26T18:59:44+00:00

Suppose I create a custom object/javascript class (airquotes) as follows: // Constructor function CustomObject(stringParam)

  • 0

Suppose I create a custom object/javascript “class” (airquotes) as follows:

// Constructor
function CustomObject(stringParam) {
  var privateProperty = stringParam;

  // Accessor
  this.privilegedGetMethod = function() {
    return privateProperty;
  }

  // Mutator
  this.privilegedSetMethod = function(newStringParam) {
    privateProperty = newStringParam;
  }
}

Then I want to make a list of those custom objects where I can easily add or remove things from that list. I decide to use objects as a way to store the list of custom objects, so I can add custom objects to the list with

var customObjectInstance1 = new CustomObject('someString');
var customObjectInstance2 = new CustomObject('someOtherString');
var customObjectInstance3 = new CustomObject('yetAnotherString');

myListOfCustomObjects[customObjectInstance1] = true;
myListOfCustomObjects[customObjectInstance2] = true;
myListOfCustomObjects[customObjectInstance3] = true;

and remove custom objects from the list with

delete myListOfCustomObjects[customObjectInstance1];

but if i try to iterate through the list with

for (i in myListOfCustomObjects) {
  alert(i.privilegedGetMethod());
}

I would get an error in the FireBug console that says “i.privilegedGetMethod() is not a function”. Is there a way to fix this problem or an idiom in javascript to do what I want? Sorry if this is a dumb question, but I’m new to javascript and have scoured the internet for solutions to my problem with no avail. Any help would be appreciated!

P.S. I realize that my example is super simplified, and I can just make the privateProperty public using this.property or something, but then i would still get undefined in the alert, and I would like to keep it encapsulated.

  • 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-05-26T18:59:44+00:00Added an answer on May 26, 2026 at 6:59 pm

    i won’t be the original object as you were expecting:

    for (i in myListOfCustomObjects) {
        alert(typeof i); // "string"
    }
    

    This is because all keys in JavaScript are Strings. Any attempt to use another type as a key will first be serialized by toString().

    If the result of toString() isn’t somehow unique for each instance, they will all be the same key:

    function MyClass() { }
    var obj = {};
    
    var k1 = new MyClass();
    var k2 = new MyClass();
    
    obj[k1] = {};
    obj[k2] = {};
    
    // only 1 "[object Object]" key was created, not 2 object keys
    for (var key in obj) {
        alert(key);
    }
    

    To make them unique, define a custom toString:

    function CustomObject(stringParam) {
        /* snip */
    
        this.toString = function () {
            return 'CustomObject ' + stringParam;
        };
    }
    
    var obj = {};
    var k1 = new CustomObject('key1');
    var k2 = new CustomObject('key2');
    
    obj[k1] = {};
    obj[k2] = {};
    
    // "CustomObject key1" then "CustomObject key2"
    for (var key in obj) {
        alert(key);
    }
    

    [Edit]

    With a custom toString, you can set the object as the serialized key and the value to keep them organized and still continue to access them:

    var customObjectInstance1 = new CustomObject('someString');
    var customObjectInstance2 = new CustomObject('someOtherString');
    var customObjectInstance3 = new CustomObject('yetAnotherString');
    
    myListOfCustomObjects[customObjectInstance1] = customObjectInstance1;
    myListOfCustomObjects[customObjectInstance2] = customObjectInstance2;
    myListOfCustomObjects[customObjectInstance3] = customObjectInstance3;
    
    for (i in myListOfCustomObjects) {
      alert(myListOfCustomObjects[i].privilegedGetMethod());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is about inheritance in JavaScript. Suppose I create a constructor Bird(), and another
Suppose you create a generic Object variable and assign it to a specific instance.
Suppose I have a user defined type: CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ...
Suppose I am designing a class that can handle any database technology to create
I am supposed to create a custom ComboBox by deriving a class from ComboBox
Suppose you have a table as follows: CREATE TABLE EMPLOYEE_SALES ( EMPLOYEE_ID NUMBER, PRODUCT_ID
public function create():ArrayCollection{ var index:int = 0; var data:ArrayCollection = new ArrayCollection(); var length:int
Suppose I create a very simple socket connection, how can one programatically: Find out
Suppose I create collection like Collection<IMyType> coll; Then I have many implelentations of IMyTypem

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.