-
-
Save getify/4062989 to your computer and use it in GitHub Desktop.
illustrating some potential issues with adding `let` to code which already has `var` used in it...
This file contains hidden or 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
// var: poor stylistic form, but no leakage | |
function foo() { | |
bar = 3; | |
if (true) { | |
var bar = 5; | |
} | |
} | |
// let: will leak an auto-global | |
function foo() { | |
bar = 3; | |
if (true) { | |
let bar = 5; | |
} | |
} |
This file contains hidden or 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
// var: common stylistic form, no leakage | |
function foo() { | |
if (true) { | |
var bar = 5; | |
} | |
bar = 3; | |
} | |
// let: will leak an auto-global | |
function foo() { | |
if (true) { | |
let bar = 5; | |
} | |
bar = 3; | |
} |
This file contains hidden or 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
// var: common stylistic form, no breakage | |
function foo() { | |
if (true) { | |
var a = 3; | |
var b = a + 2; | |
} | |
return b; | |
} | |
// let: will break | |
function foo() { | |
if (true) { | |
let a = 3; | |
let b = a + 2; | |
} | |
return b; | |
} |
This file contains hidden or 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
// var: common stylistic form, no breakage | |
function foo() { | |
if (true) { | |
var a = 3; | |
var b = a + 2; | |
} | |
return b; | |
} | |
// let: broken, certainly by accident | |
function foo() { | |
if (true) { | |
var a = 3; | |
let b = a + 2; | |
} | |
return b; | |
} | |
// let: works, probably by accident | |
function foo() { | |
if (true) { | |
let a = 3; | |
var b = a + 2; | |
} | |
return b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment