Created
April 21, 2018 13:55
-
-
Save colevandersWands/557c6f7f770eaecfcb6216c893e69166 to your computer and use it in GitHub Desktop.
lexical vs block scoping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
some 'let' vs 'var' (block vs lexical) scoping examples | |
with a bit of context mixed in | |
run these in PythonTutor for greatest learning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (true) { | |
let if_block = "if block" | |
} | |
if (true) { | |
var lexical_1 = "lexical_1" | |
} | |
lexical_1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function context_demonstrater(){ | |
var lexical_2 = "inside function"; | |
lexical_2 | |
{ | |
let block_2 = "block 2"; | |
} | |
} | |
// lexical_2 | |
context_demonstrater() | |
lexical_2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function new_context() { | |
let context_variable = "ctv" | |
{ | |
let context_scope = "cts" | |
} | |
} | |
new_context() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// polution in the global space | |
{ | |
var lexical_1 = "lexical"; | |
} | |
{ | |
var lexical_1 = "overwritten"; | |
} | |
// no polution | |
{ | |
let block = "block"; | |
block | |
} | |
{ | |
let block = "kcolb"; | |
block | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cielings(){ | |
let sinker_6 = "sinker 6"; | |
{ | |
let sinker_5 = "sinker 5"; | |
{ | |
var huh = "huh"; | |
let sinker_4 = "sinker 4"; | |
{ | |
let sinker_3 = "sinker 3"; | |
{ | |
let sinker_2 = "sinker 2"; | |
{ | |
var floater = "floater"; | |
let sinker_1 = "sinker 1"; | |
} | |
sinker_2 | |
} | |
sinker_4 | |
huh | |
} | |
floater | |
// sinker_2 | |
sinker_5 | |
} | |
sinker_6 | |
} | |
// sinker_1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment