简体   繁体   中英

How to implement event-driven JavaScript without involving any DOM element?

Is there any way to implement event-driven programming in JavaScript, without involving any DOM element? Such as creating an event-handler which executed every time an array get sorted.

Sure! The keyword you're looking for is "pubsub". Here are some well-known implementations:

But you could also do it yourself, like so:

window.pubsub = (function () {
    var eventToListeners = {};

    return {
        sub: function (event, callback) {
            if (!eventToListeners.hasOwnProperty(event)) {
                eventToListeners[event] = [];
            }
            eventToListeners[event].push(callback);
        },
        pub: function (event, args) {
            if (eventToListeners.hasOwnProperty(event)) {
                for (var i = 0; i < eventToListeners[event].length; ++i) {
                   try {
                       eventToListeners[event][i].call(null, args);
                   } catch (e) {
                       if (console && console.error) {
                           console.error(e);
                       }
                   }
                }
            }
        }
    };
}());

// Sample usage:
pubsub.sub("arraySorted", function () {
    console.log("array was sorted");
});

var myArray = [2, 3, 1];

myArray.sort();
pubsub.pub("arraySorted");

In newish browsers, we've added the ability to construct an EventTarget directly:

const et = new EventTarget();

et.addEventListener("arraySorted", () => {
  console.log("array was sorted");
});

const myArray = [2, 3, 1];

myArray.sort();
et.dispatchEvent(new Event("arraySorted"));

See https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget for more examples as well as a browser compatibility table. As of the time of this writing it only works in Chrome (64+) and Firefox (59+), but over time support will expand to include Safari and Edge.

Mixing Backbone.Events into your object would give you this. (See Backbone.js)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM