Les Scopes en JavaScript
====Scope====
In JavaScript, objects and functions are also variables.
In JavaScript, scope is the set of variables, objects, and functions you have access to.
===Local Scope===
JavaScript has function scope: variables declared within a JavaScript function, become LOCAL to the function and they can only be accessed within the function.
===Global Scope===
A variable declared outside a function, becomes GLOBAL : all scripts and functions on a web page can access it.
In HTML, the global scope is the window object. All global variables belong to the window object.
===Auto Global Scope===
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.
This code example will declare a global variable carName, even if the value is assigned inside a function.
<code>
myFunction();
// code here can use carName
function myFunction() {
carName = "Volvo";
}</code>
Commentaires
Enregistrer un commentaire