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 2015年7月.
Object.create() 指定其原型物件與屬性,創建一個新物件。
指定新物件的原型 (prototype) 物件。
propertiesObject選用,為一物件。如有指定且非 undefined,則此參數物件中可列舉出的屬性 (即參數物件自身定義的屬性,並非指原型鏈上的 enumerable 特性 ) 對應其屬性名稱,根據其屬性敘述元 (property descriptors) 加進新創建的物件。這些屬性對應到 Object.defineProperties() 的第二個參數。
具有指定原型物件與屬性的新物件。
如果 proto 參數不是 null 或一個物件,將會拋出 TypeError 例外。
下方是如何使用 Object.create() 去實現類別繼承的示範,此為 JavaScript 支援的單一繼承.。
也可像 mixin 繼承多個物件。
Object.assign 複製 OtherSuperClass 原型上的所有屬性到 MyClass 的原型上,使所有 MyClass 的實例都能使用。Object.assign 為 ES2015 標準且有 polyfill。如需支援較舊的瀏覽器,可使用第三方套件實現如 jQuery.extend() 或 .assign() 。
此 polyfill 涵蓋了主要的使用情境:指定一個原型創建一個新的物件,第二個參數為選用。
要注意的是在 ES5 的 Object.create 中,[[Prototype]] 可以為 null,但在 ECMAScript 5 以前的版本,polyfill 會因為繼承限制(limitation inherent)而不支援此情形。
| ECMAScript® 2027 Language Specification # sec-object.create |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年7月14日 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.