fonction - Quelle est la portée des variables dans JavaScript

Mots clés : javascriptfunctionvariablesscopejavascript

meilleur 3 Réponses fonction - Quelle est la portée des variables dans JavaScript

vote vote

94

function f() {     function g() {         console.log(x)     }     let x = 1     g() } f() // 1 because x is hoisted even though declared with `let`!
function f() {     var x = 1     let y = 1     const z = 1 } console.log(typeof x) // undefined (because var has function scope!) console.log(typeof y) // undefined (because the body of the function is a block) console.log(typeof z) // undefined (because the body of the function is a block)
{     var x = 1     let y = 1     const z = 1 } console.log(x) // 1 console.log(typeof y) // undefined because `y` has block scope console.log(typeof z) // undefined because `z` has block scope
for(var x = 0; x < 5; ++x) {} console.log(x) // 5 (note this is outside the loop!)
for(var x = 0; x < 5; ++x) {     setTimeout(() => console.log(x)) // closes over the `x` which is logically positioned at the top of the enclosing scope, above the loop } console.log(x) // note: visible outside the loop
for(let x = 0; x < 5; ++x) {     setTimeout(() => console.log(x)) // `let` declarations are re-declared on a per-iteration basis, so the closures capture different variables } console.log(typeof x) // undefined
if(false) {     var x = 1 } console.log(x) // here, `x` has been declared, but not initialised
for(let x = 0; x < 10; ++x) {}  console.log(typeof x) // undefined, because `x` is block-scoped
if(false) {     let x = 1 } console.log(typeof x) // undefined, because `x` is block-scoped
// module1.js  var x = 0 export function f() {}  //module2.js  import f from 'module1.js'  console.log(x) // throws ReferenceError 
var x = 1 console.log(window.hasOwnProperty('x')) // true
let x = 1 console.log(window.hasOwnProperty('x')) // false
function f(x) {} console.log(typeof x) // undefined, because `x` is scoped to the function
try {} catch(e) {} console.log(typeof e) // undefined, because `e` is scoped to the catch block
(function foo() { console.log(foo) })() console.log(typeof foo) // undefined, because `foo` is scoped to its own expression
x = 1 // implicitly defined property on the global object (no "var"!)  console.log(x) // 1 console.log(window.hasOwnProperty('x')) // true
'use strict' {     function foo() {} } console.log(typeof foo) // undefined, because `foo` is block-scoped
vote vote

84

var myVariable = "Some text"; 
let myVariable = "Some text"; 
// i IS NOT known here // j IS NOT known here // k IS known here, but undefined // l IS NOT known here  function loop(arr) {     // i IS known here, but undefined     // j IS NOT known here     // k IS known here, but has a value only the second time loop is called     // l IS NOT known here      for( var i = 0; i < arr.length; i++ ) {         // i IS known here, and has a value         // j IS NOT known here         // k IS known here, but has a value only the second time loop is called         // l IS NOT known here     };      // i IS known here, and has a value     // j IS NOT known here     // k IS known here, but has a value only the second time loop is called     // l IS NOT known here      for( let j = 0; j < arr.length; j++ ) {         // i IS known here, and has a value         // j IS known here, and has a value         // k IS known here, but has a value only the second time loop is called         // l IS NOT known here     };      // i IS known here, and has a value     // j IS NOT known here     // k IS known here, but has a value only the second time loop is called     // l IS NOT known here }  loop([1,2,3,4]);  for( var k = 0; k < arr.length; k++ ) {     // i IS NOT known here     // j IS NOT known here     // k IS known here, and has a value     // l IS NOT known here };  for( let l = 0; l < arr.length; l++ ) {     // i IS NOT known here     // j IS NOT known here     // k IS known here, and has a value     // l IS known here, and has a value };  loop([1,2,3,4]);  // i IS NOT known here // j IS NOT known here // k IS known here, and has a value // l IS NOT known here 
vote vote

80

<script>  var globalVariable = 7; //==window.globalVariable  function aGlobal( param ) { //==window.aGlobal();                              //param is only accessible in this function   var scopedToFunction = {     //can't be accessed outside of this function      nested : 3 //accessible by: scopedToFunction.nested   };    anotherGlobal = {     //global because there's no `var`   };   }  </script> 

Questions similaires