Skip to content

Instantly share code, notes, and snippets.

@addityasingh
Created April 10, 2016 20:21
Show Gist options
  • Save addityasingh/d9ecb7c2636b19144b9fa7f0251c2c46 to your computer and use it in GitHub Desktop.
Save addityasingh/d9ecb7c2636b19144b9fa7f0251c2c46 to your computer and use it in GitHub Desktop.
In JavaScript, are named callback functions hoisted?
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