0

I'm trying to add objects to an array with a specific id which I need to know inside the constructor. Will this work?

objectArray = [];
function newObject (name, age, eyeColour) {
this.name = name;
this.age = age;
this.eyeColour = eyeColour;
var id = *some integer* 
this.id = id;
objectArray[id] = this;    //This is the line in question.
}

Obviously this is just an example. In my real code I'm using the id in the id of 3 new DOM objects, so I need to have it available inside the constructor.

1
  • id is a number, and objectArray is declared globally. I'll edit to fix those issues in my example. Commented Jan 31, 2014 at 20:21

2 Answers 2

2

If id is a number this will definitely work, arrays are meant to be accessed by their index. You see this all the time in code and being in a constructor will not matter as long as objectArray is declared globally:

var arr = ["one", "two"];

for(var i =0; i < arr.length; i++){
   alert(arr[i]);
}

If the id is not a number your most likely working with an object.

var obj = {one: 1, two: 2};
alert(obj["one"]);

Here is an example using your code:

var objectArray = []

function newObject (name, age, eyeColour) {
    this.name = name;
    this.age = age;
    this.eyeColour = eyeColour;
    var id = 3
    this.id = id;
    objectArray[id] = this;    //This is the line in question.
}

newObject("Kevin", "30", "Blue");
alert(objectArray[3].name);

One thing to note about this, if your calculated id is not in sync with the actual array, so say you assign the object to the third index when the array is empty, array.length will return 4 however the array only contains 1 element.

Sign up to request clarification or add additional context in comments.

5 Comments

I think you have to put the "one" and "two" in your second example inside quotes, or am I wrong?
@maja Your thinking of JSON
@maja See: jsfiddle.net/4GLZr/1 and developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Now if the property names were key words like say class then you need to add quotes.
I always used the quotes in javascript - I never realised that they're optional, and it's much nicer without them :)
Valid JSON requires the quotes, while the object literal notation does not.
2

Yes, this works. But you should probably define your id - you are missing the keyword var. If id is not a number, but a string, it will work if you define the objectArray as an Object. (not an Array)

I'm not sure, where you defined your ObjectArray. But if you put the line var ObjectArray = {}; above, it will work. (If you're sure, that id is always a number, use var ObjectArray = []; instead.

This is the resuting code:

function newObject (name, age, eyeColour) {
    this.name = name;
    this.age = age;
    this.eyeColour = eyeColour;
    var id = *something calculated*
    this.id = id;
    var objectArray = {};   //or =[] for and Array
    objectArray[id] = this;    //This is the line in question.
}

3 Comments

Thanks! In my own tests it hadn't worked, but it turns out to have been a (different) very silly mistake on my part. Glad I asked StackOverflow instead of giving up.
If you define the objectArray inside the constructor like that, it's only local (and fairly pointless). It definitely needs to be globally defined.
In this case, you have to define it globally, as Kevin Bowersox did in his answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.