> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/obj/extend/mixin/mixin-inheritance.md).

# mixin inheritance

{% tabs %}
{% tab title="💾 程式" %}

* replit ⟩ [mixin inheritance](https://replit.com/@pegasusroe/JS-object-mixin-inheritance#index.js)

```javascript
const { log } = console;

// -------------- Mixin Inheritance --------------

// ⭐ mixin's base object
const canSaySomething = {
    say(phrase) { log(phrase) }
};

// ⭐ inherits from `canSaySomething`
const canSayHi = {
    
    // -------------------------------------
    // ⭐ prototype chain: 
    // can use Object.setPrototypeOf() instead.
    __proto__: canSaySomething,
    // -------------------------------------
    
    // -------------------------------------
    // ⭐ super 
    //     === [[HomeObject]].[[Prototype]]
    //     === canSayHi.[[Prototype]]
    //     === canSaySomething
    // -------------------------------------
    sayHi() { super.say(`Hello ${this.name}!`) },
    sayBye() { super.say(`Bye ${this.name}!`) },
};

// User
class User {
    constructor(name) {
        this.name = name;
    }
}

// --------------- mix in --------------------------
// ⭐ copy all enumerable own members from mixin(s)
//    to `User.prototype` (NOT `User`).
//
//             ╭ ⭐️ target  ╮ ╭source─╮
Object.assign( User.prototype, canSayHi );

const dude = new User("Dude");

dude.sayHi();          // Hello Dude!
dude.sayBye();         // Bye Dude!
```

{% endtab %}

{% tab title="🧨 雷區" %}

```javascript
// ------------------- 🧨 雷區 ----------------------------
// ⭐ 注意：
//   不能呼叫 mixin's base object 中的 methods❗
//   如果 dude 找不到 say()，它會往自己的 prototype chain 找，
//   而不是在 mixin 的 prototype chain 找，要特別注意❗
// -------------------------------------------------------
// ⛔ TypeError: `dude.say` is not a function
dude.say(`I'm super!`);
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] JS.info ⟩ [A mixin example](https://javascript.info/mixins#a-mixin-example)
  {% endtab %}

{% tab title="👥 相關" %}

* [super](/web/js/val/class/inheritance/super.md) is used to refer to mixin's parent object.
* mixin's methods know their [\[\[HomeObject\]\]](/web/js/val/obj/method/home-object.md).
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/obj/extend/mixin/mixin-inheritance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
