※ 引述《broo (陈爷)》之铭言:
: 标题: [问题] Hoisting 问题
: 时间: Sat Feb 18 23:48:05 2017
: 范例是这样的
: (function(){
: var test =function(){return 1;}
: function test() {return 2;}
: return test();
: })();
: 经过hoistibg后会长这样
: (function(){
: var test;
: function test() {return 2;}
: test = function() {return 1;}
: return test();
: })();
: 我怎么想结果都是2,因为最后是return test()不是吗??为什么会是1呢
: 脑筋无法转过来..
: 麻烦了 手机排版请见谅
我觉得hoisting应该不是这样解释
https://developer.mozilla.org/zh-TW/docs/Glossary/Hoisting
hoisting teaches that variable and function declarations are physically moved
to the top your coding, but this is not what happens at all. What does happen
is the variable and function declarations are put into memory during the
compile phase, but stays exactly where you typed it in your coding.
简单讲就是javascript会分creation跟execution phases
会先执行creation phases
把宣告的变量跟宣告的function存到内存
(此时变量还没被assign,所以value会是undefined)
然后再执行excution phases
就是逐行的去执行程式码