Pārlūkot izejas kodu

Finish Section 3: Variables and Flow Control

Signed-off-by: Michael Tang <michael.h.tang@gmail.com>
Michael Tang 5 gadi atpakaļ
vecāks
revīzija
7c5351737f
2 mainītis faili ar 49 papildinājumiem un 0 dzēšanām
  1. 20 0
      scope-2.js
  2. 29 0
      scope.js

+ 20 - 0
scope-2.js

@@ -0,0 +1,20 @@
+// Global ()
+  // Local ()
+    //Local (name)
+  // Local
+
+//let name = 'Andrew'
+
+if (true) {
+    
+    //let name = 'Mike'
+
+    if (true) {
+        let name = 'Jen' // leaked global
+        console.log(name)
+    }
+}
+
+if (true) {
+    console.log(name)
+}

+ 29 - 0
scope.js

@@ -0,0 +1,29 @@
+// Lexical Scope (Static Scope)
+// Global Scope - Defined outside of all code blocks
+// Local Scope - Defined inside a code block
+
+// In a scope you can access variables defined in that scope, or in any parent/ancestor scope
+
+// Global Scope  (varOne)
+  // Local Scope (varTwo)
+    // Local Scope (varFour)
+  // Local Scope (varThree)
+
+
+let varOne = 'varOne'
+
+if (true) {
+    console.log(varOne)
+    let varTwo = 'varTwo'
+    console.log(varTwo)
+
+    if (true) {
+        let varFour = 'varFour'
+    }
+}
+
+if (true) {
+    let varThree = 'varThree'
+}
+
+console.log(varTwo)