+ 3
Array dereferencing?
Is it legal to dereference an array of objects using [] and then pass it to a function? for example, function myobj(x, y) { this.x=x; this.y=y; } var objarray = new Array(); objarray[0] = myobj(0,0); objarray[1] = myobj(1,1); then pass objarray[0] to a function that accesses x? I tried but I got a type undefined error when I try to access x.
1 ответ
+ 3
Yes, you can but like this
function myobj(x, y)
{
this.x=x;
this.y=y;
}
var objarray = new Array();
objarray[0] = new myobj(5,0);
objarray[1] = new myobj(1,1);
document.write(objarray[0].x);
its output:
5
Use new keyword for creating a new object.