最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

8種現(xiàn)代JavaScript響應(yīng)式模式小結(jié)

 更新時間:2024年02月07日 10:55:35   作者:今天也想MK代碼  
響應(yīng)式的本質(zhì)是關(guān)于系統(tǒng)如何對數(shù)據(jù)變化做出反應(yīng),并且存在不同類型的響應(yīng)式,本文就來介紹8種現(xiàn)代JavaScript響應(yīng)式模式小結(jié),具有一定的參考價值,感興趣的可以了解一下

響應(yīng)式的本質(zhì)是關(guān)于系統(tǒng)如何對數(shù)據(jù)變化做出反應(yīng),并且存在不同類型的響應(yīng)式。但是,在本文中,我們的重點是關(guān)于根據(jù)數(shù)據(jù)變化采取行動的響應(yīng)式。

作為一名前端開發(fā)人員,我每天都要面對這些。這是因為瀏覽器本身是一個完全異步的環(huán)境。現(xiàn)代 web 接口必須快速響應(yīng)用戶操作,包括更新 UI,發(fā)送網(wǎng)絡(luò)請求,管理導(dǎo)航以及其他各種任務(wù)。

雖然人們經(jīng)常將響應(yīng)式與框架聯(lián)系在一起,但是我認為通過使用純 JS 實現(xiàn)它我們可以學到很多。所以,我們將自己編寫模式代碼,并研究一些基于響應(yīng)式的原生瀏覽器 API。

PubSub 或發(fā)布-訂閱

PubSub 是最常用和基本的響應(yīng)式模式之一。發(fā)布者負責通知訂閱者有關(guān)更新的信息,訂閱者接收這些更新并可以相應(yīng)地做出反應(yīng)。

class PubSub {
  constructor() {
    this.subscribers = {};
  }

  subscribe(event, callback) {
    if (!this.subscribers[event]) {
      this.subscribers[event] = [];
    }

    this.subscribers[event].push(callback);
  }

  // Publish a message to all subscribers of a specific event
  publish(event, data) {
    if (this.subscribers[event]) {
      this.subscribers[event].forEach((callback) => {
        callback(data);
      });
    }
  }
}

const pubsub = new PubSub();

pubsub.subscribe('news', (message) => {
  console.log(`Subscriber 1 received news: ${message}`);
});

pubsub.subscribe('news', (message) => {
  console.log(`Subscriber 2 received news: ${message}`);
});

// Publish a message to the 'news' event
pubsub.publish('news', 'Latest headlines: ...');

// console logs are:
// Subscriber 1 received news: Latest headlines: ...
// Subscriber 2 received news: Latest headlines: ...

一個流行的使用示例是 Redux。這個流行的狀態(tài)管理庫就是基于這種模式(或者更具體地說,是基于 Flux 架構(gòu))。在 Redux 的上下文中,事情工作得很簡單:

  • 發(fā)布者:存儲充當發(fā)布者的角色。當一個動作被分發(fā)時,存儲通知所有訂閱的組件有關(guān)狀態(tài)更改的信息。

  • 訂閱者:應(yīng)用程序中的 UI 組件是訂閱者。它們訂閱 Redux 存儲并在狀態(tài)發(fā)生變化時接收更新。

作為 PubSub 瀏覽器版本的自定義事件

瀏覽器通過 CustomEvent 類和 dispatchEvent 方法為觸發(fā)和訂閱自定義事件提供了 API。后者不僅為我們提供了觸發(fā)事件的能力,還可以將任何所需的數(shù)據(jù)附加到事件上。

const customEvent = new CustomEvent('customEvent', {
  detail: 'Custom event data', // Attach desired data to the event
});

const element = document.getElementById('.element-to-trigger-events');

element.addEventListener('customEvent', (event) => {
  console.log(`Subscriber 1 received custom event: ${event.detail}`);
});

element.addEventListener('customEvent', (event) => {
  console.log(`Subscriber 2 received custom event: ${event.detail}`);
});

// Trigger the custom event
element.dispatchEvent(customEvent);

// console logs are:
// Subscriber 1 received custom event: Custom event data
// Subscriber 2 received custom event: Custom event data

自定義事件目標

如果你不喜歡在 window 對象上全局分派事件,你可以創(chuàng)建自己的事件目標。

通過擴展原生 EventTarget 類,您可以將事件分派到它的新實例上。這可以確保您的事件只在新類本身上被觸發(fā),避免全局傳播。此外,您可以靈活地直接將處理程序附加到此特定實例上。

class CustomEventTarget extends EventTarget {
  constructor() {
    super();
  }

  // Custom method to trigger events
  triggerCustomEvent(eventName, eventData) {
    const event = new CustomEvent(eventName, { detail: eventData });
    this.dispatchEvent(event);
  }
}

const customTarget = new CustomEventTarget();

// Add an event listener to the custom event target
customTarget.addEventListener('customEvent', (event) => {
  console.log(`Custom event received with data: ${event.detail}`);
});

// Trigger a custom event
customTarget.triggerCustomEvent('customEvent', 'Hello, custom event!');

// console log is:
// Custom event received with data: Hello, custom event!

觀察者

觀察者模式與 PubSub 真的很相似。您訂閱 Subject,然后它通知其訂閱者(Observers)有關(guān)更改的信息,允許它們相應(yīng)地作出反應(yīng)。這種模式在構(gòu)建解耦和靈活的架構(gòu)方面發(fā)揮著重要作用。

class Subject {
  constructor() {
    this.observers = [];
  }

  addObserver(observer) {
    this.observers.push(observer);
  }

  // Remove an observer from the list
  removeObserver(observer) {
    const index = this.observers.indexOf(observer);

    if (index !== -1) {
      this.observers.splice(index, 1);
    }
  }

  // Notify all observers about changes
  notify() {
    this.observers.forEach((observer) => {
      observer.update();
    });
  }
}

class Observer {
  constructor(name) {
    this.name = name;
  }

  // Update method called when notified
  update() {
    console.log(`${this.name} received an update.`);
  }
}

const subject = new Subject();
const observer1 = new Observer('Observer 1');
const observer2 = new Observer('Observer 2');

// Add observers to the subject
subject.addObserver(observer1);
subject.addObserver(observer2);

// Notify observers about changes
subject.notify();

// console logs are:
// Observer 1 received an update.
// Observer 2 received an update.

使用 Proxy 的響應(yīng)式屬性

如果您想對對象中的更改做出反應(yīng),Proxy 是最好的選擇。它允許我們在設(shè)置或獲取對象字段的值時實現(xiàn)響應(yīng)式。

const person = {
  name: 'Pavel',
  age: 22,
};

const reactivePerson = new Proxy(person, {
  // Intercept set operation
  set(target, key, value) {
    console.log(`Setting ${key} to ${value}`);
    target[key] = value;

    // Indicates if setting value was successful
    return true;
  },
  // Intercept get operation
  get(target, key) {
    console.log(`Getting ${key}`);

    return target[key];
  },
});

reactivePerson.name = 'Sergei'; // Setting name to Sergei
console.log(reactivePerson.name); // Getting name: Sergei

reactivePerson.age = 23; // Setting age to 23
console.log(reactivePerson.age); // Getting age: 23

單個對象屬性和響應(yīng)式

如果您不需要跟蹤對象中的所有字段,可以使用 Object.defineProperty 或 Object.defineProperties 來選擇特定的一個或一組。

const person = {
  _originalName: 'Pavel', // private property
}

Object.defineProperty(person, 'name', {
  get() {
    console.log('Getting property name')
    return this._originalName
  },
  set(value) {
    console.log(`Setting property name to value ${value}`)
    this._originalName = value
  },
})

console.log(person.name) // 'Getting property name' and 'Pavel'
person.name = 'Sergei' // Setting property name to value Sergei

使用 MutationObserver 的響應(yīng)式 HTML 屬性

使用 MutationObserver 實現(xiàn) DOM 中的響應(yīng)式的一種方法。它的 API 允許我們觀察目標元素及其子元素中的屬性更改和文本內(nèi)容更改。

function handleMutations(mutationsList, observer) {
  mutationsList.forEach((mutation) => {
    // An attribute of the observed element has changed
    if (mutation.type === 'attributes') {
      console.log(`Attribute '${mutation.attributeName}' changed to '${mutation.target.getAttribute(mutation.attributeName)}'`);
    }
  });
}

const observer = new MutationObserver(handleMutations);
const targetElement = document.querySelector('.element-to-observe');

// Start observing the target element
observer.observe(targetElement, { attributes: true });

使用 IntersectionObserver 的響應(yīng)式滾動

IntersectionObserver API 使我們能夠響應(yīng)目標元素與另一個元素或視口區(qū)域的相交。

function handleIntersection(entries, observer) {
  entries.forEach((entry) => {
    // The target element is in the viewport
    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');

// Start observing the target element
observer.observe(targetElement);

到此這篇關(guān)于8種現(xiàn)代JavaScript響應(yīng)式模式小結(jié)的文章就介紹到這了,更多相關(guān)JavaScript響應(yīng)式模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 關(guān)于iframe跨域POST提交的方法示例

    關(guān)于iframe跨域POST提交的方法示例

    這篇文章主要給大家介紹了關(guān)于iframe跨域POST提交的相關(guān)資料,文中給出了詳細的介紹與示例代碼,相信對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以一起來學習學習。
    2017-01-01
  • 根據(jù)對象的某一屬性進行排序的js代碼(如:name,age)

    根據(jù)對象的某一屬性進行排序的js代碼(如:name,age)

    實例為按降序排列,若想改為升序只需把比較器中的value2-value1改為value1-value2就可以了
    2010-08-08
  • js中區(qū)分深拷貝與淺拷貝的實戰(zhàn)過程

    js中區(qū)分深拷貝與淺拷貝的實戰(zhàn)過程

    兩個對象A、B,A有數(shù)據(jù)B為空,B復(fù)制了A,我們修改A,如果B中的數(shù)據(jù)跟著變化了,那就是淺拷貝,如果沒有變化,那就是深拷貝,下面這篇文章主要給大家介紹了關(guān)于js中區(qū)分深拷貝與淺拷貝的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • JavaScript如何使用插值實現(xiàn)圖像漸變

    JavaScript如何使用插值實現(xiàn)圖像漸變

    這篇文章主要介紹了JavaScript如何使用插值實現(xiàn)圖像漸變,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Javascript獲取某個月的天數(shù)

    Javascript獲取某個月的天數(shù)

    本文給大家分享使用getDaysInOneMonth(year, month)方法獲取某個月的天數(shù),方法超簡單,感興趣的朋友一起看看吧
    2018-05-05
  • 20個JS簡寫技巧提升工作效率

    20個JS簡寫技巧提升工作效率

    這篇文章主要介紹了20個JS簡寫技巧提升工作效率,本文特點以言簡意賅為主,圍繞JS簡寫技巧的話題展開全文,具有一定的查看價值 ,需要的小伙伴可以參考一下
    2021-12-12
  • 深入理解JavaScript內(nèi)存管理和GC算法

    深入理解JavaScript內(nèi)存管理和GC算法

    這篇文章主要介紹了深入理解JavaScript內(nèi)存管理和GC算法,下面文章主要講解JavaScript的垃圾回收機制以及常用的垃圾回收算法;還講解了V8引擎中的內(nèi)存管理,最后介紹了Performance工具如何使用
    2022-07-07
  • javascript+HTML5的canvas實現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼

    javascript+HTML5的canvas實現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼

    這篇文章主要介紹了javascript+HTML5的canvas實現(xiàn)七夕情人節(jié)3D玫瑰花效果代碼,使用了html5的canvas技術(shù),可呈現(xiàn)出帶有3D效果的玫瑰花漸顯效果,非常逼真自然,需要的朋友可以參考下
    2015-08-08
  • javascript實現(xiàn)滾動效果的數(shù)字時鐘實例

    javascript實現(xiàn)滾動效果的數(shù)字時鐘實例

    這篇文章主要是介紹使用javascript來實現(xiàn)數(shù)字時鐘滾動的效果,非常實用,有需要的朋友們可以來參考學習。
    2016-07-07
  • js 輪播效果實例分享

    js 輪播效果實例分享

    本文主要分享了基于js實現(xiàn)的輪播效果的實例代碼,具有一定的參考價值,下面跟著小編一起來看下吧
    2016-12-12

最新評論

肇源县| 永宁县| 临泽县| 龙海市| 毕节市| 香港 | 中西区| 青阳县| 吉安市| 横峰县| 安塞县| 湘潭县| 云霄县| 都昌县| 盐边县| 平远县| 广丰县| 大丰市| 方正县| 佛山市| 枣强县| 静安区| 大新县| 平乡县| 额敏县| 舟曲县| 榕江县| 巫山县| 新龙县| 施甸县| 柏乡县| 泊头市| 无锡市| 邻水| 扎囊县| 乌鲁木齐县| 湖南省| 固始县| 宜州市| 林周县| 汉中市|