Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年3月.
constructor(建構子)是個隨著 class 一同建立並初始化物件的特殊方法。
一個 class 只能有一個稱為 constructor 的特殊物件。如果一個 class 出現兩次以上的 constructor,就會發生 SyntaxError 錯誤。
如果不指定建構子,就會使用預設的建構子。
這段程式碼是從 classes sample 擷取而來。(線上範例)
如上文所說:如果不指定建構子,就會使用預設的建構子。對 base classes 而言,預設的建構子長得像這樣:
對 derived class 而言,預設的建構子長得像這樣:
| ECMAScript® 2027 Language Specification # sec-static-semantics-constructormethod |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月16日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.