響應性本質上是關于系統(tǒng)如何對數(shù)據(jù)變化作出反應,有不同類型的響應性。然而,在這篇文章中,我們關注的是響應性,即響應數(shù)據(jù)變化而采取行動。
作為一名前端開發(fā)者,Pavel Pogosov 每天都要面對這個問題。因為瀏覽器本身是一個完全異步的環(huán)境。現(xiàn)代 Web 界面必須快速響應用戶的操作,這包括更新 UI、發(fā)送網(wǎng)絡請求、管理導航和執(zhí)行各種其他任務。
盡管人們常常將響應性與框架聯(lián)系在一起,Pavel Pogosov 認為通過純 JavaScript 實現(xiàn)響應性可以學到很多。所以,我們將自己編寫一些模式代碼,并研究一些基于響應性的原生瀏覽器 API。
class PubSub { constructor() { this.subscribers = {}; } subscribe(event, callback) { if (!this.subscribers[event]) { this.subscribers[event] = []; } this.subscribers[event].push(callback); } // 向特定事件的所有訂閱者發(fā)布消息 publish(event, data) { if (this.subscribers[event]) { this.subscribers[event].forEach((callback) => { callback(data); }); } }}const pubsub = new PubSub();pubsub.subscribe('news', (message) => { console.log(`訂閱者1收到了新聞:${message}`);});pubsub.subscribe('news', (message) => { console.log(`訂閱者2收到了新聞:${message}`);});// 向 'news' 事件發(fā)布消息pubsub.publish('news', '最新頭條新聞:...');// 控制臺日志輸出:// 訂閱者1收到了新聞:最新頭條新聞:...// 訂閱者2收到了新聞:最新頭條新聞:...
一個常見的使用示例是 Redux。這款流行的狀態(tài)管理庫基于這種模式(或更具體地說,是 Flux 架構)。在 Redux 的上下文中,工作機制相當簡單:
發(fā)布者:store 充當發(fā)布者。當一個 action 被派發(fā)時,store 會通知所有訂閱的組件狀態(tài)的變化。 訂閱者:應用程序中的 UI 組件是訂閱者。它們訂閱 Redux store 并在狀態(tài)變化時接收更新。
瀏覽器通過 CustomEvent 類和 dispatchEvent 方法提供了一個用于觸發(fā)和訂閱自定義事件的 API。后者不僅能讓我們觸發(fā)事件,還能附加任何想要的數(shù)據(jù)。
const customEvent = new CustomEvent('customEvent', { detail: '自定義事件數(shù)據(jù)', // 將所需數(shù)據(jù)附加到事件});const element = document.getElementById('.element-to-trigger-events');element.addEventListener('customEvent', (event) => { console.log(`訂閱者1收到了自定義事件:${event.detail}`);});element.addEventListener('customEvent', (event) => { console.log(`訂閱者2收到了自定義事件:${event.detail}`);});// 觸發(fā)自定義事件element.dispatchEvent(customEvent);// 控制臺日志輸出:// 訂閱者1收到了自定義事件:自定義事件數(shù)據(jù)// 訂閱者2收到了自定義事件:自定義事件數(shù)據(jù)
如果你不想在全局 window 對象上分派事件,可以創(chuàng)建你自己的事件目標。
通過擴展原生 EventTarget 類,你可以向其新實例分派事件。這確保你的事件僅在新類本身上觸發(fā),避免了全局傳播。此外,你可以直接將處理程序附加到這個特定實例上。
class CustomEventTarget extends EventTarget { constructor() { super(); } // 觸發(fā)自定義事件的自定義方法 triggerCustomEvent(eventName, eventData) { const event = new CustomEvent(eventName, { detail: eventData }); this.dispatchEvent(event); }}const customTarget = new CustomEventTarget();// 向自定義事件目標添加事件監(jiān)聽器customTarget.addEventListener('customEvent', (event) => { console.log(`自定義事件收到了數(shù)據(jù):${event.detail}`);});// 觸發(fā)自定義事件customTarget.triggerCustomEvent('customEvent', '你好,自定義事件!');// 控制臺日志輸出:// 自定義事件收到了數(shù)據(jù):你好,自定義事件!
觀察者模式與 PubSub 非常相似。你訂閱 Subject,然后它通知其訂閱者(觀察者)關于變化,使他們能夠做出相應的反應。這種模式在構建解耦和靈活的架構中發(fā)揮了重要作用。
class Subject { constructor() { this.observers = []; } addObserver(observer) { this.observers.push(observer); } // 從列表中移除觀察者 removeObserver(observer) { const index = this.observers.indexOf(observer); if (index !== -1) { this.observers.splice(index, 1); } } // 通知所有觀察者關于變化 notify() { this.observers.forEach((observer) => { observer.update(); }); }}class Observer { constructor(name) { this.name = name; } // 通知時調用的更新方法 update() { console.log(`${this.name} 收到了更新。`); }}const subject = new Subject();const observer1 = new Observer('觀察者1');const observer2 = new Observer('觀察者2');// 將觀察者添加到主體subject.addObserver(observer1);subject.addObserver(observer2);// 通知觀察者關于變化subject.notify();// 控制臺日志輸出:// 觀察者1 收到了更新。// 觀察者2 收到了更新。
如果你想對對象的變化做出反應,Proxy 是一個好方法。它讓我們在設置或獲取對象字段的值時實現(xiàn)響應性。
const person = { name: 'Pavel', age: 22,};const reactivePerson = new Proxy(person, { // 攔截設置操作 set(target, key, value) { console.log(`將 ${key} 設置為 ${value}`); target[key] = value; // 表示設置值是否成功 return true; }, // 攔截獲取操作 get(target, key) { console.log(`獲取 ${key}`); return target[key]; },});reactivePerson.name = 'Sergei'; // 將 name 設置為 Sergeiconsole.log(reactivePerson.name); // 獲取 name: SergeireactivePerson.age = 23; // 將 age 設置為 23console.log(reactivePerson.age); // 獲取 age: 23
如果你不需要跟蹤對象中的所有字段,可以使用 Object.defineProperty 或一組 Object.defineProperties 來選擇特定的一個或幾個。
const person = { _originalName: 'Pavel', // 私有屬性}Object.defineProperty(person, 'name', { get() { console.log('獲取屬性 name') return this._originalName }, set(value) { console.log(`將屬性 name 設置為值 ${value}`) this._originalName = value },})console.log(person.name) // '獲取屬性 name' 和 'Pavel'person.name = 'Sergei' // 將屬性 name 設置為值 Sergei
在 DOM 中實現(xiàn)響應性的一種方法是使用 MutationObserver。其 API 允許我們觀察目標元素及其子元素的屬性變化和文本內容變化。
function handleMutations(mutationsList, observer) { mutationsList.forEach((mutation) => { // 觀察到的元素的一個屬性發(fā)生了變化 if (mutation.type === 'attributes') { console.log(`屬性 '${mutation.attributeName}' 更改為 '${mutation.target.getAttribute(mutation.attributeName)}'`); } });}const observer = new MutationObserver(handleMutations);const targetElement = document.querySelector('.element-to-observe');// 開始觀察目標元素observer.observe(targetElement, { attributes: true });
IntersectionObserver API 允許對目標元素與另一個元素或視口區(qū)域的交集做出反應。
function handleIntersection(entries, observer) { entries.forEach((entry) => { // 目標元素在視口中 if (entry.isIntersecting) { entry.target.classList.add('visible'); } else { entry.target.classList.remove('visible'); } });}const observer = new IntersectionObserver(handleIntersection);const targetElement = document.querySelector('.element-to-observe');// 開始觀察目標元素observer.observe(targetElement);
本文鏈接:http://www.tebozhan.com/showinfo-26-91830-0.html八種現(xiàn)代 JavaScript 響應式模式
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
下一篇: Spring中都應用了哪些設計模式呢?