Created
April 10, 2016 20:21
-
-
Save addityasingh/d9ecb7c2636b19144b9fa7f0251c2c46 to your computer and use it in GitHub Desktop.
In JavaScript, are named callback functions hoisted?
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 enclosingScope () { | |
var b; | |
function inner (def) { | |
def(); | |
} | |
var a = 2; | |
} | |
// After hoisting due to compilation, the above changes to | |
function enclosingScope () { | |
// Function declarations are hoisted before variables | |
function inner (def) { | |
def(); | |
} | |
var b, a; | |
a = 2 | |
} | |
// But if I have a named callback, will that be hoisted? | |
function enclosingScope () { | |
function inner (def) { | |
def(); | |
} | |
var b, a; | |
a = 2 | |
inner(function cb () { | |
console.log('Test callback hoisting') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment