By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Inner object problem property undefined error

P: n/a
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);
}

}

Mar 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

On Mar 22, 6:42 am, "00steve" <steven_macl...@hotmail.comwrote:
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);

}
}

Mar 22 '07 #2

P: n/a
On 22 Mar, 14:21, "Jason" <redund...@gmail.comwrote:
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

On Mar 22, 6:42 am, "00steve" <steven_macl...@hotmail.comwrote:
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.

Mar 23 '07 #3

P: n/a
On Mar 22, 8:42 pm, "00steve" <steven_macl...@hotmail.comwrote:
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>
--
Rob

Mar 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.