Function

基本宣告函式

function square(number){
    return number * number
}
// 在() 中的為,參數(Arguments)
// 在{} 中的為,函式功能內部的主要區塊

定義函式的方式

  • 函式宣告(同上)

  • 函式運算式

const NewNumber = function(number){
    return number * number
}
// 此稱匿名函式
  • 透過 new Function建立函式(運作效能較差)

const NewNumber = new Function('number' , return number * number)

變數的有效範圍(Scope)

  • ES6前,JS 變數有效範圍的最小單位是以 function

  • ES6後,有 let const 分別定義變數與常數。他們的 scope 是用 {} 來切分的

關於函數的 Hoisting

Hoisting

Last updated

Was this helpful?