Implicitly declared variables and prototypes

trying to save it. Using phpstorm to view my code and get some errors.

He says my function names have "Variable Implicitly Declared"

function towngate10() { updateDisplay(locations[10].description); if (!gate10) { score = score+5; gate10 = true; } playerLocation = 10; displayScore(); document.getElementById("goNorth").disabled=true; document.getElementById("goSouth").disabled=false; document.getElementById("goEast").disabled=true; document.getElementById("goWest").disabled=true; } 

Also, I just want to make sure I’ve worked on my prototypes correctly. Just a sample.

Global arrays:

 var locations = [10]; locations[0] = new Location(0,"Intersection","This is where you awoke."); locations[1] = new Location(1,"Cornfield","The cornfields expand for miles."); locations[2] = new Location(2,"Lake","A large lake that is formed by a river flowing from the East.", new Item(1,"Fish","An old rotting fish.")); locations[3] = new Location(3,"Outside Cave","Entrance to the dark cave."); 

Location Function:

 function Location(id, name, description, item) { this.id = id; this.name = name; this.description = description; this.item = item; this.toString = function() { return "Location: " + this.name + " - " + this.description; } } 
+6
source share
1 answer

Relatively implicitly declared variables:

 if (!gate10) { score = score+5; gate10 = true; } 

and

 playerLocation = 10; 

score, gate and playerLocation are created as global variables. Phpstorm will warn you about this. If they are not intended for global access, declare variables with var instead. This will make the variables local only to the area in which it was created:

 if (!gate10) { var score = score+5; var gate10 = true; } 

and

 var playerLocation = 10; 

I suggest you familiarize yourself with the change in variables . Global variables can leave spaces in your safety if they are not handled properly.

+4
source

Source: https://habr.com/ru/post/985742/


All Articles