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
히어운트예츠트
히어운트예츠트
건축학 -> 경제학 -> 호주 워홀 -> 해외영업 -> 개발자 -> ?
  • 히어운트예츠트
    기술블로그
    히어운트예츠트
  • 전체
    오늘
    어제
    • All About Programming;)
      • Other than Tech
        • 잡생각
      • Computer Science
        • Terminology and Concepts
        • Network
        • Operating System
        • Database
        • Data Structure
        • Web Development
        • Interview Prep
      • Frontend
        • Javascript Essentials
        • Perfomance Optimization
        • JS Patterns
        • React
        • Next.js
        • Flutter
        • Testing
      • Backend
        • Node.js
      • DevOps
        • Docker & Kubernetes
      • Coding Test
        • LeetCode
        • 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
        • Nativ_2026
      • 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
  • 블로그 메뉴

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

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

  • 인기 글

  • 태그

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

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
히어운트예츠트
Mixin pattern
상단으로

티스토리툴바