Provides EventTarget interface to any object
npm install oo-eventtargetjavascript
var $EventTarget = require("oo-eventtarget");
var Product = function(name, price) {
$EventTarget(this); // add mixin to the product
this.name = name;
this.price = price;
};
Product.prototype.buy = function(quantity) {
// dispatch a "buy" event
this.dispatchEvent("buy", { quantity: quantity });
};
// listen to a product
var product = new Product("Millenium Falcon", 120);
product.addEventListener("buy", function(event) {
console.log(event.type, event.data.quantity, event.target.name + "s");
});
product.buy(2); // logs "buy 2 Millenium Falcons"
``