Skip to content

Instantly share code, notes, and snippets.

@colevandersWands
Created April 21, 2018 13:55
Show Gist options
  • Save colevandersWands/557c6f7f770eaecfcb6216c893e69166 to your computer and use it in GitHub Desktop.
Save colevandersWands/557c6f7f770eaecfcb6216c893e69166 to your computer and use it in GitHub Desktop.
lexical vs block scoping
some 'let' vs 'var' (block vs lexical) scoping examples
with a bit of context mixed in
run these in PythonTutor for greatest learning
if (true) {
let if_block = "if block"
}
if (true) {
var lexical_1 = "lexical_1"
}
lexical_1
function context_demonstrater(){
var lexical_2 = "inside function";
lexical_2
{
let block_2 = "block 2";
}
}
// lexical_2
context_demonstrater()
lexical_2
function new_context() {
let context_variable = "ctv"
{
let context_scope = "cts"
}
}
new_context()
// polution in the global space
{
var lexical_1 = "lexical";
}
{
var lexical_1 = "overwritten";
}
// no polution
{
let block = "block";
block
}
{
let block = "kcolb";
block
}
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