首頁
學習紀錄
遊戲心得影視Life書單案件檔案
Side Projects委託作品與二創互動實驗場
Kurau
百百 BLOG
首頁
學習紀錄
遊戲心得影視Life書單案件檔案
Side Projects委託作品與二創互動實驗場
Kurau

Kurau Blog

「隨心而寫,真真假假,都是我」

一個記錄生活、輸出興趣的個人空間。
遊戲、影視、閱讀、學習……每一段體驗都值得留下文字。

頁面導覽

  • 學習紀錄
  • 遊戲心得
  • 影視Life
  • 書單
  • 委託作品與二創
  • Kurau
  • 合作邀請

找到我

歡迎來 Discord 找我聊天!

“曾經發生的事不可能忘記,只是暫時想不起來而已。”-《神隱少女》

© 2026 Kurau All rights reserved

面試考題

箭頭函數 和 一般函數的 差別

By Kurau·Updated 2026-05-09·3 分鐘閱讀

頭函數和普通函數的差別如下:

  1. 語法:箭頭函數的語法比較簡潔,可以省去很多不必要的符號,使代码更容易閱讀。普通函數則需要使用 **function** 關鍵字,並且必須定義函數名。

  2. **this** 綁定:箭頭函數的 **this** 是固定的,不受外界的影響,因此它不适用于需要修改 **this** 的场景。普通函數的 **this** 是可以通过 **call**、**apply** 或 **bind** 方法修改的。

  3. 參數:箭頭函數的參數與普通函數的參數相同,都可以接受多個參數。

  4. 回傳:箭頭函數和普通函數的回傳方式相同,都可以使用 **return** 關鍵字回傳數值。

因此,根據需要使用的功能,您可以選擇使用箭頭函數或普通函數。

使用普通函數的情況:

  1. 修改 **this**:如果需要修改函數的 **this**,則應使用普通函數。
    在這個程式碼中,在 **showName** 方法中,使用普通函數作為 **setTimeout** 的回調函數,但由於普通函數在這種情況下會改變 **this** 的指向,因此會得到 **undefined**。因此,調用 **myClass.showName()** 會印出:undefined
class MyClass {
  constructor() {
    this.name = "My Class";
  }

  showName() {
    setTimeout(function() {
      console.log(this.name);
    }, 1000);
  }
}

const myClass = new MyClass();
myClass.showName(); // undefined
JavaScript

如果您想要在普通函數中引用類實例中的屬性,您可以在構造函數中保存實例,並在普通函數中引用它:

class MyClass {
  constructor() {
    this.name = "My Class";
    this.self = this;
  }

  showName() {
    setTimeout(function() {
      console.log(this.self.name);
    }, 1000);
  }
}

const myClass = new MyClass();
myClass.showName(); // My Class
JavaScript
  1. 使用 arguments 參數:如果需要使用 arguments 參數,則應使用普通函數。
function sum() {
  let total = 0;
  for (let i = 0; i < arguments.length; i++) {
    total += arguments[i];
  }
  return total;
}

console.log(sum(1, 2, 3, 4, 5));
JavaScript

使用箭頭函數的情況:

  1. 簡化代码:如果需要簡化函數的語法,則應使用箭頭函數。
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(number => number * number);
console.log(squares);
JavaScript
  1. 保留外部的 **this**:如果需要保留外部的 **this**,則應使用箭頭函數。

class MyClass {
  constructor() {
    this.name = "My Class";
  }

  showName() {
    setTimeout(() => {
      console.log(this.name);
    }, 1000);
  }
}

const myClass = new MyClass();
myClass.showName();
JavaScript

目錄

    ◆ 相關文章

    • 箭頭函數 Arrow function expression

      2026-06-02
    • JavaScript的各種函式

      2026-05-09
    • Function Component 介紹

      2026-05-09
    • Functional Programming介紹

      2026-05-09
    ← 上一篇物件導向程式設計基本原則 - SOLID下一篇 →網頁上實現markdown編輯

    ◆ 關於作者

    Kurau

    個人寫作 / 創作的 SoT,記錄遊戲、影視、學習與生活。

    更多 Kurau 的文章