Last updated 2 years ago
class A extends Object 與 class A 最大的不同就是:
改變繼承路線,繼承了 Object (而不是 Function.prototype)
變成 derived class (而不是 base class),因此在 constructor (如果有的話) 內必須呼叫 super。
replit ⟩ class A vs class A extends Object
// ------------------------------------------- // ⭐ B can access Object's "static methods", // ⭐ but A can't❗ // ------------------------------------------- class A {} class B extends Object {} const obj = { name: 'Joe', age: 21 }; // ⛔ TypeError: `A.getOwnPropertyNames` is not a function❗ A.getOwnPropertyNames(obj); // ⭐ normally call `Object.getOwnPropertyNames(obj)` directly. B.getOwnPropertyNames(obj), // [ 'name', 'age' ]
JS.info ⟩ Class extends Object?
MDN ⟩ JavaScript ⟩
JavaScript Guide ⟩ Working with objects
built-in objects ⟩
Object
Function