mhomami / Tank

0 stars 0 forks source link

Use `const` right where variables are defined #2

Open andyg0808 opened 2 years ago

andyg0808 commented 2 years ago

https://github.com/mhomami/Tank/blob/74e115e25ae04a530c524b3d3756b83eb56cb745/js/script.js#L39

  1. I'd suggest using const instead of let whenever possible, because it makes it clear that the variable only ever has one value. Of course, if you need to change the value over time (as happens with _w and _h, for example), then you'll need to use let.
  2. I'd suggest declaring variables right where you use them, because it keeps the declaration together with the code that uses the variable.
mhomami commented 2 years ago

I changed most of the let to const excepting the resize function variables and my delta variable. However, I would have thought I could declare line 39 as const but this seems to break the canvas. I tried separating the bits from the drivers and the jimmy thinking it may have something to do with the array but it still breaks. Any thoughts? Is this something to do with the x and y coordinate changes as part of the animating functions of PIXI?

andyg0808 commented 2 years ago

Looks like it's not legal to declare a const ahead of time: image

(I tried it out in my browser console to see what would happen)

Try dropping line 39 altogether and put the consts on lines 48, 52, etc.

mhomami commented 2 years ago

Gotcha thank you! I'll have a look at this now.