Reputation: 261
This is regarding using OO concepts in jQuery.
I want to create an array of structures in jQuery so that I can save a char and an integer in same the element of an array.
I want to store 'Y' and 2 in each element of array, so that I can change the value of the integer based on whether the value of the char is 'Y'.
I have the following code
var arrOfArr = new Array(3);
for (var x = 0; x < arrOfArr.length; x++) {
arrOfArr[x] = [new struct('Y', 0),new struct('Y', 0),new struct('Y', 0)];
}
function struct($user, $turnCount) {
var user = $user;
var turnCount = $turnCount;
}
Now when I call alert(arrOfArr[0][0].turnCount)
, it is giving undefined
. But if I define arrOfArr[0][0].turnCount = 0
and again call the same alert it gives me 0.
Can you please help me understand how can I initialize an array of structures at the time of initialization?
Upvotes: 2
Views: 271
Reputation: 14620
var user
and
var turnCount
are private variables to structs scope
try using
this.user = $user;
this.turnCount = $turncount;
Upvotes: 1
Reputation: 262939
You're assigning to local variables instead of instance properties in your constructor.
Try writing:
function struct($user, $turnCount) {
this.user = $user;
this.turnCount = $turnCount;
}
Upvotes: 3