We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hoisting
讓 Variable 和 Function 還沒宣告就可以使用。
只有宣告沒有賦值,變數的值就是 undefined。 只有賦值沒有宣告,變數會在 Global Scope 做宣告。
test() function test() { console.log(1) }
test() var test = function() { console.log(1) }
console.log(test) var test =10 function test() { console.log(1) }
test() var test = 10 function test() { console.log(1) } function test() { conosle.log(2) }
function test(a) { console.log(a) var a = 30 } test(10)
function test(a) { console.log(a) let a = 30 } test(10)
function test(a) { //before console.log(a) var a = 30 function a() { } } test(10)
function test(a) { //after function a() {} console.log(a) a = 30 } test(10)
function test(b) { // before console.log(a) var a = 30 function b() { } } test(15)
function test(b) { // after function b() {} var a console.log(a) a = 30 } test(15) // 問題:如果裡面有設定名字是 b 的 function 那還會引入外面的變數嗎?
function test(d) { //before var d = 20 function test2(c) { console.log(d) var d = 30 } test2(15) } test(10)
function test(d) { // after var d = 20 function test2(c) { var c =15 var d console.log(d) d = 30 } test2(15) } test(10)
function test(d) { // before function test2(c) { console.log(d) } var d = 20 test2(15) } test(10)
function test(d) { // after var d function test2(c) { var c = 15 console.log(d) } d = 20 test2(15) } test(10)
function test(d) { // before function test2(c) { console.log(d) } test2(15) var d = 20 }ß test(10)
function test(d) { //after var d = 10 function test2(c) { var c = 15 console.log(d) } test2(15) d = 20 } test(10)
function test() { console.log(a) let a = 1 } test()
function test() { let a = 1 function test2() { console.log(a) } test2() } test()
function test() { //before let a = 1 function test2() { console.log(a) var a = 10 } test2() } test()
function test() { //after let a = 1 function test2() { var a console.log(a) a = 10 } test2() } test()
function test() { // before let a = 1 function test2() { console.log(a) let a = 10 } test2() } test()
function test() { // after function test2() { // Temporal Dead Zone var 是 undefined 用let 是 not defined console.log(a) a = 10 } test2() } test()
《我知道你懂 hoisting,可是你了解到多深?》
The text was updated successfully, but these errors were encountered:
No branches or pull requests
宣告提升
Hoisting
理解
Hoisting
這個概念可以學到什麼?為什麼要設計宣告提升的機制?
讓 Variable 和 Function 還沒宣告就可以使用。
宣告分為兩個階段
只有宣告沒有賦值,變數的值就是 undefined。
只有賦值沒有宣告,變數會在 Global Scope 做宣告。
提升步驟
如果 變數 和 function 用相同的名字,誰先宣告誰先用。(這點好像有問題 例題3,8)(function 和 variable 誰先拿到最上面?)例題
1
is not a function
因為只有變數 test 有提升, function 是一個函式表示式(匿名函式)(有名字的人才會被點出來?)
function
2
10
has already been declared
function
undefined
a is not defined
1
undefined
a is not defined
參考資料
延伸閱讀
《我知道你懂 hoisting,可是你了解到多深?》
The text was updated successfully, but these errors were encountered: