Created
February 5, 2019 08:32
-
-
Save ktajpuri/c8ae7e0c548cffd69da9c60ec11478e8 to your computer and use it in GitHub Desktop.
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
// Say you have this function | |
function demo() { | |
var a = 1; | |
let b = 2; | |
if (1) { | |
var c = 3; | |
let d = 4; | |
console.log(a, b, c, d); | |
} | |
console.log(a, b, c, d); | |
} | |
// if we execute demo() it will give a ReferenceError: d is not defined. This is because d being defined by let wont be available | |
// outside the scope of if. If we define it with var it works fine. How does babel make us get this reference error for the first case? | |
// We need to define the block within an IFFE | |
// This works fine | |
function demo() { | |
var a = 1; | |
let b = 2; | |
if (1) { | |
var c = 3; | |
var d = 4; | |
console.log(a, b, c, d); | |
} | |
console.log(a, b, c, d); | |
// This will give e refereence error for d as it would give if d was defined with let. | |
function demo() { | |
var a = 1; | |
let b = 2; | |
if (1) { | |
var c = 3; | |
(function() { | |
var d = 4; | |
console.log(a, b, c, d); | |
})(); | |
} | |
console.log(a, b, c, d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment