Reputation: 8163
Hello all I am working in javascript and html5.I want to ask that how can we add an array as a data member of a class in javascript I have written a code
var bm = new Bitmap(img); //It is a built in class of some library
/*Here what I want is to associate an array with object of bitmap*/
/* what i did is */
var lpr = new Array();
bm.lpr[0]= "xyz" ;
bm.lpr[1]= "pqr" ;
but when I displayed the array.
alert(bm.lpr[0]);
I got the error
Uncaught TypeError: Cannot set property '0' of undefined
can any one please tell me the correct way of doing it.Also my array will be update at run time
Thanks
Upvotes: 0
Views: 2348
Reputation: 2495
You only have to change one line:
var bm = new Bitmap(img);
bm.lpr = new Array(); //this is the line you need to change
bm.lpr[0]= "xyz" ;
bm.lpr[1]= "pqr" ;
alert(bm.lpr[0]);
Upvotes: 1
Reputation: 490173
If you want a property of the bm
instance to hold an array, you can do that like so...
bm.lpr = ['xyz', 'pqr'];
Your alert()
will then show what you want.
Upvotes: 2