-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
Variable hoisting in Javascript describes the usage of variables within a scope. There are different scopes in Javascript and the language supports lexical scoping but not block scoping which means that variables are function-scoped rather than block-scoped.
An example for function scoped could be:
function outerFunction()
{
for(var i = 0; i < 5; i++)
{
n = i * 2;
}
var n;
function innerFunction()
{
var t = 10;
}
//n = 8 & i = 5
alert("n:" + n + ", i:" + i );
//ReferenceError: t is not defined
alert("t:" + t);
}
outerFunction();
There are in sum three variables within outerFunction. Two of those can be reached by it, but innerFunction does not allow an access to its own declared variables, because of function scoped property. In consequence there will be a ReferenceError when trying to access varaible t. Also variable n is used before it is declared which does not result into an error.
In short - Hoisting is the behaviour of Javascript-interpreter by how it handles declaration of variables and functions within functions.
The user interaction part should look like the content as seen below by starting "index.html" in a web browser.
The essential part of the program relevant to hoisting is found in "hoisting.js" and are commented as ATTENTION.
The game is very simple and works as following:
- Left picture describes the player
- Right picture describes the opponent
- Player has 100 Hitpoints and competitor has 500
- Strength of player and competitor is randomly generated by pressing the "START ROUND!" button
- Placed beneath the button is a short description of the recent round
Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman