+ 2

Should I avoid global variables in js?

To let objects from different classes interact I have created them with global variables. Is there a more elegant / better practice solution in js? Can I call one object from the method of another class? The code has commented the code I would like to move. Gravity between Attractor and Mover. https://code.sololearn.com/WBO51FhQdm7d/?ref=app

11th Jul 2020, 4:39 AM
bell
bell - avatar
12 Antworten
+ 2
bell Have tried passing the Attractor object to the Mover object's update() method? https://code.sololearn.com/WU5If5RBaZmy/#js
11th Jul 2020, 10:48 AM
ODLNT
ODLNT - avatar
+ 8
You can keep them as class variables and at the time of calling methods from other class pass it as argument.
11th Jul 2020, 4:46 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
Ore you mean declaring movers and attractors as extensions of the App class? I have only tried interaction between the two classes I have but could not make it work. thank you!
11th Jul 2020, 6:38 AM
bell
bell - avatar
+ 2
ODLNT Thank you so much! This is exactly what I was missing, but it will also only work if I declare the attractor and movers globally, right?
11th Jul 2020, 11:47 AM
bell
bell - avatar
+ 1
🔫 Rick Grimes Thank you! i have tried but if I am in class mover, it does not seem to know the Attractor object if I do not declare it in init. I post the code because I have not been able to make it work any other way.
11th Jul 2020, 4:51 AM
bell
bell - avatar
+ 1
It is not wrong to use global variables this way. However have you considered create another class to handle the variabes. Something like class App { constructor () { this.ctx = document.querySelector("#cvs").getContext("2d"); this.W = ctx.canvas.width = innerWidth; this.H = ctx.canvas.height = innerHeight; this.p = []; this.a = {}; this.D = H*1.; this.col = ....; } Dist (x1) pick() Norm() } So in your init, create a new App instance and pass that around method calls.
11th Jul 2020, 6:29 AM
Ore
Ore - avatar
+ 1
Yes exactly. I know that may not sit well with functional programmers but there should also be functional ways to solve this.
11th Jul 2020, 6:43 AM
Ore
Ore - avatar
+ 1
My question was more specific to the code I have made. i found a solution to have both classes see each other's objects but I do not know how to make a method from one class see an object from another. I have tried for a couple of days. I would like to know if it is possible or if I need to derive both classes from the same base for this to work. Eventually I want to have light and particles separated.
11th Jul 2020, 7:02 AM
bell
bell - avatar
+ 1
bell Tou can keep the object instances as global constant variables. Like this const app = new App() You don't have to worry about mutability because it is constant. The attributes of the object can be changed and used in any context. 😉 Is that what you are asking?
11th Jul 2020, 7:20 AM
Ore
Ore - avatar
+ 1
Ore this is sort of what I am doing, wheter with a new clas or purely global it is not that different. if you look into the code I have attached to my question, I started with attract being a method of attractor class, rather than a global function. I need help to know if and how this can be called from a method of movers class. Attract needs position and mass of both objects. thank you
11th Jul 2020, 7:44 AM
bell
bell - avatar
+ 1
bell It is not possible except you have access to the Attractor object from the Mover class. You can either 1. Make the Attractor object global 2. Pass the Attractor object instance to the Mover constructor and store the Attractor object has a Mover attribute. 3. Also you can make the needed Attractor fields static (Note: that this is not widely supported across browsers)
11th Jul 2020, 8:23 AM
Ore
Ore - avatar
+ 1
bell, your welcome. With your code as-is, "a" is global. Since you didn't use var, let or const to declare the variable "a" as an object, it becomes a global variable automatically. https://code.sololearn.com/WdxrQOHSBMMr/# Yes your code will also work with mover and attractor as global objects
11th Jul 2020, 12:41 PM
ODLNT
ODLNT - avatar