mainajaved
mainajaved

Reputation: 8163

How to declare array as a data member of a class in JavaScript

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

Answers (2)

Avi Y
Avi Y

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

alex
alex

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

Related Questions