JSFiddle - React, Tailwind, and code Playground
HTML
<button onclick='return test()'>Click me</button>
JavaScript
function arEventManager() {
this.callbacks = {};
this.addCallback = function(eventCategory, fn) {
if (!this.callbacks[eventCategory]) {
this.callbacks[eventCategory] = [];
}
if (fn instanceof Function) {
this.callbacks[eventCategory].push(fn);
}
return this;
}, // addCallback
this.dispatchEvent = function(eventCategory, params) {
// Callback-Funktion(en) ausloesen
for (var iC = 0, lC = this.callbacks[eventCategory].length; iC < lC; iC++) {
console.log( this.callbacks[eventCategory][iC] );
this.callbacks[eventCategory][iC](params);
}
} // dispatchEvent
};
function arPerson() {
this.name;
this.setName = function(name) {
this.name = name;
},
this.getName = function() {
return (this.name);
},
this.onEvent = function(p2) {
alert('this.name = ' + this.name + ' / ' + 'p2.name = ' + p2.name);
}
};
var eventManager = new arEventManager;
var Thomas = new arPerson();
Thomas.setName('Thomas');
var Mike = new arPerson();
Mike.setName('Mike');
eventManager.addCallback("myEvent", Mike.onEvent.bind(Mike));
function test() {
eventManager.dispatchEvent('myEvent', Thomas);
}