+ 2

Best way to pass an obj from a class as parameter to the constructor of another class js

In this code Movers should evolve through a GA. I pack the traits to evolve in a Gene object (so as to add more traits) and pass it to the Mover class constructor. Can I keep the Gene object inside the Mover object or do I need to use it to create variables in the constructor? Is there a better alternative in which I can use this.gen.dx etc directly in Mover? My code works but I would like to know the proper way, please. ChillPill https://code.sololearn.com/W00hhxF38m4w/?ref=app

13th Aug 2020, 9:07 PM
bell
bell - avatar
2 ответов
+ 3
yes but I could not make it work the way you say ( I tried first). Should I be able to refer to this.gen.ax then? Is it ok to instantiate Gene objects inside and outside the Movers object? I think my implementation is not the proper style. Besides, if my gene object grows I will need to change Mover. Ideally, I would want to isolate all genetic traits in Gene. Thank you!
14th Aug 2020, 8:09 AM
bell
bell - avatar
+ 2
Assign gen (parameter) to `this.gen` (instance variable) in constructor of `Mover`. ``` class Mover { constructor(x, y, radius, gen) { . . . this.gen = gen; } ``` Is this what you are looking for?
14th Aug 2020, 4:13 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar