Mixin pattern

2024. 12. 8. 09:49·Frontend/JS Patterns

An object used to

Add reusable functionality to another object or class, without using inheritance.

Simply, adding functionalities to the target prototype!


  1. We can add the dogFunctionality mixin to the Dog prototype with the Object.assign method.
  2. We can add animalFunctionality to the dogFunctionality prototype, using Object.assign. In this case, the target object is dogFunctionality.
class Dog {
  constructor(name) {
    this.name = name;
  }
}

const animalFunctionality = {
  walk: () => console.log("Walking!"),
  sleep: () => console.log("Sleeping!")
};

const dogFunctionality = {
  __proto__: animalFunctionality,
  bark: () => console.log("Woof!"),
  wagTail: () => console.log("Wagging my tail!"),
  play: () => console.log("Playing!"),
  walk() {
    super.walk();
  },
  sleep() {
    super.sleep();
  }
};

Object.assign(Dog.prototype, dogFunctionality);

const pet1 = new Dog("Daisy");

console.log(pet1.name);
pet1.bark(); 
pet1.play(); 
pet1.walk();
pet1.sleep();

An example of a mixin is Window interface in a browser environment.

The Window object implements many of its properties from the WindowOrWorkerGlobalScope and WindowEventHandlers mixins, which allow us to have access to properties such as setTimeout and setInterval, indexedDB, and isSecureContext.

'Frontend > JS Patterns' 카테고리의 다른 글

Observer pattern  (0) 2024.12.08
Module pattern  (0) 2024.12.08
Middleware Pattern  (0) 2024.12.08
Mediator Pattern  (0) 2024.12.08
Flyweight Pattern  (0) 2024.12.08
'Frontend/JS Patterns' 카테고리의 다른 글
  • Observer pattern
  • Module pattern
  • Middleware Pattern
  • Mediator Pattern
JTB
JTB
웹/앱 개발 정보를 공유하고 있습니다.
  • JTB
    JTechBlog
    JTB
  • 전체
    오늘
    어제
    • All About Programming;) N
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • Next.js
        • Flutter
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test N
        • LeetCode N
        • Programmers
      • Tech Books & Lectures
        • Javascript_Modern JS Deep d..
        • Network_IT 엔지니어를 위한 네트워크 입문
      • Projects
        • PolyLingo_2025
        • Build Your Body_2024
        • JStargram_2021
        • Covid19 Tracker_2021
        • JPortfolio_2021
      • BootCamp_Codestates
        • TIL
        • TILookCloser
        • Pre Tech Blog
        • IM Tech Blog
        • Daily Issues and DeBugging
        • First Project
        • Final Project
        • Sprint Review
        • Good to Know
        • Socrative Review
        • HTML & CSS
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 글쓰기
    • 관리
  • 공지사항

  • 인기 글

  • 태그

    CPU scheduling algorithm
    mobile app
    polylingo
    VoiceJournal
    DOM
    Network
    Threads and Multithreading
    모던 자바스크립트 Deep Dive
    Time complexity and Space complexity
    Data Structure
    자바스크립트
    need a database
    Binary Tree BFS
    TCP/IP
    딥다이브
    testing
    이벤트
    스코프
    How memory manage data
    database
    indie hacker
    js pattern
    자바스크립트 딥다이브
    Shared resources
    JavaScript
    structure of os
    커리어
    leetcode
    Operating System
    프론트엔드 성능 최적화 가이드
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
JTB
Mixin pattern
상단으로

티스토리툴바