Inner object problem property undefined error

0

00steve

Hi, I am attempting to create a script in which object A contains an
array of "objectb" objects. An overview of the code is posted below.
When I attempt to access "myObjArr" array like this:

alert(test1.myObjArr[0].a);

I get the error "myObjArr is undefined". I was wondering if anyone
could help.
Thanks.


test1 = new objectA(x,y);
test1.addObj(a,b,c);

alert(test1.myObjArr[0].a); //generates error


function objectA(x,y) {

this.addObj = addObj;
this.myObjArr = new Array();


function objectB(a,b,c) {
this.a =a;
}


function addObj() {
myObjArr[myObjArr.length] = new objectB(a,b,c);
}

}
 
J

Jason

I didn't get an error on the alert, I got one on the
"test1.addObj(a,b,c);"

test1 = new objectA(x,y);
test1.addObj(a,b,c);

alert(test1.myObjArr[0].a); //generates error

function objectA(x,y) {
this.addObj = addObj;
this.myObjArr = new Array();
}

function objectB(a,b,c) {
this.a =a;
}

function addObj(a,b,c) {
this.myObjArr.push(new objectB(a,b,c));
}

That worked fine for me.

You were missing the "this" on the myObjArr in the addObj function so
it was thinking that myObjArr was a normal variable which didn't
exist.

Hope this helps.

-jason
 
0

00steve

I didn't get an error on the alert, I got one on the
"test1.addObj(a,b,c);"

test1 = new objectA(x,y);
test1.addObj(a,b,c);

alert(test1.myObjArr[0].a); //generates error

function objectA(x,y) {
this.addObj = addObj;
this.myObjArr = new Array();

}

function objectB(a,b,c) {
this.a =a;

}

function addObj(a,b,c) {
this.myObjArr.push(new objectB(a,b,c));

}

That worked fine for me.

You were missing the "this" on the myObjArr in the addObj function so
it was thinking that myObjArr was a normal variable which didn't
exist.

Hope this helps.

-jason

Hi, I am attempting to create a script in which object A contains an
array of "objectb" objects. An overview of the code is posted below.
When I attempt to access "myObjArr" array like this:
alert(test1.myObjArr[0].a);

I get the error "myObjArr is undefined". I was wondering if anyone
could help.
Thanks.
test1 = new objectA(x,y);
test1.addObj(a,b,c);
alert(test1.myObjArr[0].a); //generates error
function objectA(x,y) {
this.addObj = addObj;
this.myObjArr = new Array();
function objectB(a,b,c) {
this.a =a;
}
function addObj() {
myObjArr[myObjArr.length] = new objectB(a,b,c);
}
}- Hide quoted text -

- Show quoted text -

Thanks for that. Looks like I was missing the obvious.
 
R

RobG

Hi, I am attempting to create a script in which object A contains an
array of "objectb" objects. An overview of the code is posted below.
When I attempt to access "myObjArr" array like this:

alert(test1.myObjArr[0].a);

I get the error "myObjArr is undefined". I was wondering if anyone
could help.
Thanks.

test1 = new objectA(x,y);
test1.addObj(a,b,c);

alert(test1.myObjArr[0].a); //generates error

function objectA(x,y) {

this.addObj = addObj;
this.myObjArr = new Array();

function objectB(a,b,c) {
this.a =a;
}

function addObj() {
myObjArr[myObjArr.length] = new objectB(a,b,c);

}
}

That looks a bit confused. Try:

<script type="text/javascript">

function objectA(x, y) {
this.myObjArr = [];
}

objectA.prototype.addObj = function (a, b, c) {
this.myObjArr.push(new objectB(a, b, c));
}

function objectB(a, b, c) {
this.a = a;
}

var test1 = new objectA('x', 'y');
test1.addObj('a', 'b', 'c');
alert(test1.myObjArr[0].a);

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top