javascript - Function hoisting in js -
function mymethod(){ alert("global mymethod"); } function mysecondmethod(){ alert("global mysecondmethod"); } function hoisting(){ alert(typeof mymethod); alert(typeof mysecondmethod); mymethod(); // local mymethod mysecondmethod(); // typeerror: undefined not function // mymethod , implementation hoisted function mymethod(){ alert("local mymethod"); } // variable mysecondmethod get's hoisted var mysecondmethod = function() { alert("local mysecondmethod"); }; } hoisting();
i not able understand how hoisting works in case , why alert("local mysecondmethod");
not shown. if can show me sequence helpful
inside hoisting
function code gets reordered follows:
function hoisting(){ var mysecondmethod; function mymethod(){ alert("local mymethod"); } alert(typeof mymethod); alert(typeof mysecondmethod); mymethod(); mysecondmethod(); mysecondmethod = function() { alert("local mysecondmethod"); }; }
here pretty obvious, create new variable mysecondmethod
inside function's scope, overlays outside definition. @ point of call of function, however, not defined (yet) , errors.