Skip to content

Instantly share code, notes, and snippets.

@getify
Created November 13, 2012 00:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save getify/4062989 to your computer and use it in GitHub Desktop.
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...
// 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;
}
}
// 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;
}
// 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;
}
// 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