Should I Declare A Variable Using LET Globally For A Function That Runs Constantly?
Solution 1:
If your variable is declared in the global scope, then it doesn't really matter if you use let
or var
.
These are functionally identical:
let myVar = 123;
function doStuff() {
console.log(myVar);
}
doStuff();
var myVar = 123;
function doStuff() {
console.log(myVar);
}
doStuff();
The difference between var
and let
becomes significant when you're declaring them in blocks:
if(true) {
var foo = 1;
}
if(true) {
let bar = 2;
}
console.log("var foo:", foo);
console.log("let bar:", bar);
As you can see, the let
declaration is restricted to its wrapping block scope. The var
declaration ignores block scope.
Solution 2:
Looks like you are attempting to maintain a character's (hero) state in multiple locations. This will become more and more difficult to maintain in a global scope as each character's actions / states will add to the global variables.
As per @jeff-huijsmans suggestion, I believe you should maintain the state inside your game
object.
This could be defined in a few ways:
game.state.firePaused
- This locks your game state to a single character, but will better contain the state of a character shooting.game.hero.firePaused
- This allows each character to maintain their own shooting state. This also has the added benefit of being able to add more characters with firing states.
As an aside, it looks like most of the answers here are attempting to address the scope issue. Defining variables globally and attempting to maintain a state outside of a function becomes very difficult to understand/read/test. There will be a lot of opinions on this topic. Fortunately for your root issue you can avoid this by using your pre-existing state object.
Solution 3:
This question really has nothing to do with let
vs. var
, per se - it's about scope in general.
Variables should be declared in the smallest scope that keeps the program functional. Global variables should be a last resort.
So, in your case, you don't need a Global variable to achieve your goal of not re-declaring the variable upon each function call. You just need to create another scope. Since all code should be kept out of the Global scope in the first place, your code should already have at least one sub-scope, which is often achieved with an Immediately Invoked Function Expression, which creates the "Module Pattern":
(function(){
let firePaused = false; // This is scoped to the entire module, but not Global
function actOnKeyPress() {
if (rightPressed) {
game.hero.rotate(game.hero.speed);
} else if (leftPressed) {
game.hero.rotate(-game.hero.speed);
}
if (!firePressed) {
firePaused = false;
}
if (firePressed && options.numberOfBullets > 0) {
if (!firePaused) {
fireBullet();
firePaused = true;
}
}
}
})();
Solution 4:
No, you should not create a global variable (and not with let
anyway).
Yes, you should declare it outside of the function if you want to have it shared between calls.
You can use a closure for that, with any kind of module pattern - from ES6 module to IIFE to simple block scope.
// ES6 module
let firePaused = false;
export function actOnKeyPress() {
// use `firePaused`
}
// IIFE
var actOnKeyPress = (function() {
let firePaused = false;
return function actOnKeyPress() {
// use `firePaused`
};
}());
// block scope
var actOnKeyPress;
{
let firePaused = false;
actOnKeyPress = function actOnKeyPress() {
// use `firePaused`
};
}
Post a Comment for "Should I Declare A Variable Using LET Globally For A Function That Runs Constantly?"