YCYa Chuinyachuh.hashnode.dev·Oct 15, 2023 · 2 min readTypeScript - 型別斷言(Type Assertion)型別斷言是 TypScript 的一種機制,允許我們手動指定一個明確的型別,提供 TypeScript 編譯器無法自行推斷的額外型別資訊。 我們可以透過這兩種寫法來指定型別斷言: 語法 尖括號 <>(angle-bracket)語法 語法:<type>value - 在需要斷言的變數前加上 <Type> 即可 const myCanvas = <HTMLCanvasElement>document.getElementById("main_canvas") as 語法 語法:value as...00
YCYa Chuinyachuh.hashnode.dev·Oct 3, 2023 · 4 min readJavaScript - 函式中的參數(parameters)與引數(arguments)參數 (parameters) vs. 引數 (arguments) 先來看看 MDN 的說明: 參數 parameter:A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. 引數 argument:An argument is a value (primitive or object) passed as...00
YCYa Chuinyachuh.hashnode.dev·Oct 3, 2023 · 6 min readJavaScript - Scope 作用域 & Closure 閉包Scope 作用域 留言 什麼是作用域 「作用域就是一個變數的生存範圍,一旦出了這個範圍,就無法存取到這個變數」。 當我們把變數 a 宣告在 function 中,function 之外的地方都無法取用這個變數: // 把變數宣告在 function 中 function test1() { var a = "hello" } console.log(a) // Uncaught ReferenceError: a is not defined 但當我們把變數 b 宣告在全域,fu...00
YCYa Chuinyachuh.hashnode.dev·Oct 3, 2023 · 3 min readJavaScript - this 是誰、指向哪裡,以及 call、apply、bindthis 是什麼 this 是 JavaScript 的一個關鍵字 this 是 function 執行時,自動生成的一個內部物件 隨著 function 執行場合的不同,this 所指向的值,也會有所不同 this 與 function 在何處被宣告完全無關,而是取決於 function 被呼叫的方式 在大多數的情況下, this 代表的就是呼叫 function 的物件 (owner Object of the function) 當 function 是某個 object 的 ...00
YCYa Chuinyachuh.hashnode.dev·Oct 3, 2023 · 3 min readJavaScript - Hoisting 提升Hoisting 是怎麼發生的? 變數和函數的宣告會在編譯階段就被放入記憶體,但實際位置和程式碼中完全一樣。 從這段 MDN 對於 hoisting 的說明大概可以了解到,Javascript 在執行程式碼之前會先進行編譯,而在編譯的過程中會將變數宣告以及函式宣告提升 (hoist) 到該 scope 的頂端,但需注意這邊並非實際改動程式碼的位置。 JS 在運作時是分成「編譯」和「執行」兩個步驟。而 hoisting 是發生在編譯的階段。 JS 在編譯的階段會將變數及函式的宣告處理好(h...00