|
| 1 | +--- |
| 2 | +chapter : js101 |
| 3 | +section: 11 |
| 4 | +title: Scope |
| 5 | +attribution: jQuery Fundamentals |
| 6 | +--- |
| 7 | + |
| 8 | +"Scope" refers to the variables that are available to a piece of code at a |
| 9 | +given time. A lack of understanding of scope can lead to frustrating debugging |
| 10 | +experiences. |
| 11 | + |
| 12 | +When a variable is declared inside of a function using the var keyword, it is |
| 13 | +only available to code inside of that function -- code outside of that function |
| 14 | +cannot access the variable. On the other hand, functions defined inside that |
| 15 | +function will have access to to the declared variable. |
| 16 | + |
| 17 | +Furthermore, variables that are declared inside a function without the var |
| 18 | +keyword are not local to the function -- JavaScript will traverse the scope |
| 19 | +chain all the way up to the window scope to find where the variable was |
| 20 | +previously defined. If the variable wasn't previously defined, it will be |
| 21 | +defined in the global scope, which can have extremely unexpected consequences. |
| 22 | + |
| 23 | +<div class="example" markdown="1"> |
| 24 | +Functions have access to variables defined in the same scope |
| 25 | + |
| 26 | + var foo = 'hello'; |
| 27 | + |
| 28 | + var sayHello = function() { |
| 29 | + console.log(foo); |
| 30 | + }; |
| 31 | + |
| 32 | + sayHello(); // logs 'hello' |
| 33 | + console.log(foo); // also logs 'hello' |
| 34 | +</div> |
| 35 | + |
| 36 | +<div class="example" markdown="1"> |
| 37 | +Code outside the scope in which a variable was defined does not have access to the variable |
| 38 | + |
| 39 | + var sayHello = function() { |
| 40 | + var foo = 'hello'; |
| 41 | + console.log(foo); |
| 42 | + }; |
| 43 | + |
| 44 | + sayHello(); // logs 'hello' |
| 45 | + console.log(foo); // doesn't log anything |
| 46 | +</div> |
| 47 | + |
| 48 | +<div class="example" markdown="1"> |
| 49 | +Variables with the same name can exist in different scopes with different values |
| 50 | + |
| 51 | + var foo = 'world'; |
| 52 | + |
| 53 | + var sayHello = function() { |
| 54 | + var foo = 'hello'; |
| 55 | + console.log(foo); |
| 56 | + }; |
| 57 | + |
| 58 | + sayHello(); // logs 'hello' |
| 59 | + console.log(foo); // logs 'world' |
| 60 | +</div> |
| 61 | + |
| 62 | +<div class="example" markdown="1"> |
| 63 | +Functions can "see" changes in variable values after the function is defined |
| 64 | + |
| 65 | + var myFunction = function() { |
| 66 | + var foo = 'hello'; |
| 67 | + |
| 68 | + var myFn = function() { |
| 69 | + console.log(foo); |
| 70 | + }; |
| 71 | + |
| 72 | + foo = 'world'; |
| 73 | + |
| 74 | + return myFn; |
| 75 | + }; |
| 76 | + |
| 77 | + var f = myFunction(); |
| 78 | + f(); // logs 'world' -- uh oh |
| 79 | +</div> |
| 80 | + |
| 81 | +<div class="example" markdown="1"> |
| 82 | +Scope insanity |
| 83 | + |
| 84 | + // a self-executing anonymous function |
| 85 | + (function() { |
| 86 | + var baz = 1; |
| 87 | + var bim = function() { alert(baz); }; |
| 88 | + bar = function() { alert(baz); }; |
| 89 | + })(); |
| 90 | + |
| 91 | + console.log(baz); // baz is not defined outside of the function |
| 92 | + |
| 93 | + bar(); // bar is defined outside of the anonymous function |
| 94 | + // because it wasn't declared with var; furthermore, |
| 95 | + // because it was defined in the same scope as baz, |
| 96 | + // it has access to baz even though other code |
| 97 | + // outside of the function does not |
| 98 | + |
| 99 | + bim(); // bim is not defined outside of the anonymous function, |
| 100 | + // so this will result in an error |
| 101 | +</div> |
0 commit comments