user677275
user677275

Reputation: 77

operate on object with private member function in javascript

I'm basically trying to create a generic object in javascript that I can use to act like an associative array.
But I would like to give the object some custom members to be able to operate on the giving object. Needles to say I'm failing miserably hopefully someone can point me in the right direction.

Here is the generic object I'm talking about.

function easeArray() {
    this.valIndex = function(searchTerm,object) {
        //searchTerm+object;
        if (searchTerm in object) {
            return "true" + object[searchTerm]
        }
    }
}

So then I try to initialize it like this.

     var  myCollection = new easeArray(); 
     myCollection = {"dioxanes": 0,  "shunning": 1,  "plowed": 2,
         "hoodlumism": 3, "cull": 4,      "learnings": 5,
        "transmutes": 6, "cornels": 7,   "undergrowths": 8,
        "hobble": 9,     "peplumed": 10, "fluffily": 11,
        "leadoff": 12,   "dilemmas": 13, "firers": 14,
        "farmworks": 15, "anterior": 16, "flagpole": 17};

this works fine but when I try to do something like this:

  alert(myCollection.valIndex())

I get an error. Type Error: object does not support property or method.

Never mind that I'm not passing the values into the function I understand that, the function is not even recognize.

My end result is to have a generic object that i can initialize and fill up with key value pairs and then simply say object.valindex("searchTerm");

Any help would be greatly appreciated.

Upvotes: 0

Views: 74

Answers (2)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

This cannot work because on latest statement you destroy the instance of your easeArray object. Thus the array has not such method defined

try this code instead

var easeArray = function(obj) {
    this.obj = obj
    this.valIndex = function(searchTerm) {
        if (searchTerm in this.obj) {
            return "true" + this.obj[searchTerm];  
        } 
    }
}

var myCollection = new easeArray({
        "dioxanes": 0,  "shunning": 1,  "plowed": 2,
        "hoodlumism": 3, "cull": 4,      "learnings": 5,
        "transmutes": 6, "cornels": 7,   "undergrowths": 8,
        "hobble": 9,     "peplumed": 10, "fluffily": 11,
        "leadoff": 12,   "dilemmas": 13, "firers": 14,
        "farmworks": 15, "anterior": 16, "flagpole": 17
    });

    myCollection.valIndex("firers"); /* this returns "true14" */

Upvotes: 1

Pointy
Pointy

Reputation: 413737

In this line of code:

     var  myCollection = new easeArray(); 

You set the variable "myCollection" to a reference to a new object you've constructed. Then, in the next line:

     myCollection = {"dioxanes": 0,  "shunning": 1,  "plowed": 2, ...

you set the same variable to a reference to a different object. The one you constructed in the first statement is gone at that point.

What you'll need to do is add some methods to your "easeArray" class to allow you to store values into it according to your needs. Given the examples you've posted, I'm not sure why you need that "valIndex" function. If you simply reference one of those strings:

     var index = myCollection["dioxanes"];

you'll see that "index" is 0.

Upvotes: 1

Related Questions