javascript - Hoisting does not work? -
i stumbled upon jsfiddle code sample hoisting https://jsfiddle.net/phusick/h4bh2m4y/
fn1('hello'); fn2('world'); function fn1(message) { console.log('mgs1:', message); } var fn2 = function(message) { console.log('msg2:', message); }
surprisingly not work :
uncaught typeerror: fn2 not function
is there error in program ?
your code equivalent this:
// function declarations hoisted function fn1(message) { console.log('mgs1:', message); } // variable declarations hoisted var fn2; fn1('hello'); fn2('world'); // assignments variable not hoisted fn2 = function(message) { console.log('msg2:', message); }
the declaration of fn1
function hoisted.
the declaration of fn2
variable hoisted.
the assignment fn2
variable not hoisted.
declarations hoisted, assignments or initializations not.
function assignments doing fn2
different function declarations such fn1
. assignments variables var
not hoisted, definition of variable hoisted. so, @ point try call fn2('world')
, fn2
still undefined
.
Comments
Post a Comment