0

I wanna create array of method in JavaScript i.e. var molecule = new array Tmolecule any recommendations?

I wanna create array of method in JavaScript i.e. var molecule = new array Tmolecule Where Tmolecule is: function TMolecules (r,m,x,y,vx,vy,clr) { this.r = r; this.m = m; this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.clr = clr; this.Erase = function () { Canvas.Pen.Color=BGC; Canvas.Brush.Color=BGC; Canvas.Ellipse(Trunc(x-r+0.5),Trunc(y-r+0.5),Trunc(x+r+0.5),Trunc(y+r+0.5)); } } var molecule = new Array Tmolecule; https://code.sololearn.com/W8D8adVIrkCu/?ref=app

15th Jan 2019, 7:44 AM
Amir Rafati
Amir Rafati - avatar
2 Answers
+ 3
You need to instantiate an array and assign each element of the array with new instance of TMolecules Eg. var molecules = []; // instantiate an array for(i=0; i<100; i++) { molecules[i] = new TMolecules(); // assign to instant of TMolecules } To init the properties of a molecule instance, assign the correct molecules with the selected index Eg. int sel = 20; // num 19th molecule molecules[sel].r=1; molecules[sel].MoveTo(10,20); molecules[sel].init(1,1,0,0,0,0,"red");
15th Jan 2019, 9:47 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 2
Special Thanks BRO for your help
15th Jan 2019, 9:50 AM
Amir Rafati
Amir Rafati - avatar