some oo-problems i think:TypeError, x has no properties

Mar 5, 2007 at 2:44pm

some oo-problems i think:TypeError, x has no properties

hi,

i’m trying to make an array of objects, each changeable and easy to query, which is the reason i chose JS over regular max.
everything worked fine on one onject, but with an array things go wrong, i’m propbably missing something basic; all my function calls give the same error as in the title.
i’m using the o’reilly book, i know several people on here use it, so a pointer there might be useful.
here’s the code:

//globals
inlets = 1;
outlets = 2;
autowatch = 1;
post(“init”);

//netholder
netsize = 100;

var net = new Array(netsize);
for (var i = 0; i < netsize; i++){
net[i] = new node(i);
}

//startnode
function node(i){
}

//create array in object
node.connectArray = new Array(100);

//adding a property to the object
node.depth = 1;

net[1].connectArray[1] = 1;
var x = net[1].connectArray[1];
post(x);//error!

i hope this isolates the problem clearly.
btw is there a way to import code as in java? it’s not very practical to have everything in a single textfile.

any help of course greatly apreciated, thanks!

isjtar

#30634
Mar 5, 2007 at 4:33pm

Hi,
Try ‘this’ instead ;)

//globals
inlets = 1;
outlets = 2;
autowatch = 1;
post(“init”);

//netholder
netsize = 100;

var net = new Array(netsize);
for (var i = 0; i < netsize; i++){
net[i] = new node(i);
}

//startnode
function node(i){
//create array in object
this.connectArray = new Array(100);
//adding a property to the object
this.depth = 1;
}

net[1].connectArray[1] = 1;
var x = net[1].connectArray[1];
post(x);//error!

#98191
Mar 5, 2007 at 4:41pm

Quote: _ben wrote on Mon, 05 March 2007 09:33
—————————————————-
> Hi,
> Try ‘this’ instead ;)
>
—————————————————-
thanks, i’ll try that rightaway, but i should be able to do it from outside the function or am i mistaken?
or can i only change properties from outside the function and add them from inside?

#98192
Mar 5, 2007 at 6:45pm

Quote: Isjtar wrote on Mon, 05 March 2007 09:41
—————————————————-
> Quote: _ben wrote on Mon, 05 March 2007 09:33
> —————————————————-
> > Hi,
> > Try ‘this’ instead ;)
> >
> —————————————————-
> thanks, i’ll try that rightaway, but i should be able to do it from outside the function or am i mistaken?
> or can i only change properties from outside the function and add them from inside?
>
—————————————————-
or maybe only an instance of a function?

#98193

You must be logged in to reply to this topic.