JavaScript Event Listeners

JavaScript Event Listeners
Event Listeners are a core part of using JavaScript in the browser and in some cases the server. So, what is an Event Listener?



What Is An Event Listener?
An event listener is a JavaScript function that listens to when a specified event is emitted/occurs on a webpage; this also applies to server-side JavaScript with certain classes in Node.js. Events are often marked by a string indicating the event name/symbol. Then, the function listens to when that symbol is emitted somewhere on the page or in your Node.js application. Now, we know what it is, let's go into detail on how it works.



Event Listener - Gritty Details
An event listener has three core components; the listener, event, and the emitter.

Listener
Listeners are the JavaScript function you pass in, which often take a parameter passed from the emitter; on web pages, this is often the DOM element's "eventTarget" that provides an object with information about where the event occurs. Now, these listener functions are stored within an object inside the event listener for later execution. Now, that leads me to how they're stored -- events.

Events
Events are symbol names that correspond to the event you want to run/listen to. You can have multiple listeners attached to a single event. And, because you can have multiple listeners for a single event, those listeners must be listed under the symbol name.

Here's an example to picture the structure of the object.



Now, this isn't valid JavaScript code, but it emphasizes the structure of an event listener. Besides, you can't effectively look at the attached listeners of a DOM object. But, as you can see it's simply an object with symbol names mapped to functions to be executed. Which leads me to the emitter.

Emitter
Events are emitted, thus it's a function/method on the event listener itself that runs each added listener. Using the structure above, it selects the symbol from the underlying object and then executes each function the list. This emit method is then tied to the specific methods and processes that would emit the corresponding event. For example, when you click on a DOM element, the element still runs the emit method for the "onclick" event. Now, explanations are nice, but here's an example of an event listener I created in the Node.js style. The same applies similarly to the event listeners in the browser.

Now, explanations are nice, but here's an example of an event listener I created in the Node.js style. The same applies similarly to the event listeners in the browser.



In this example, we have a class called EventEmitter; this mimics a style similar to the Node.js EventEmitter class. The idea is similar to the event listeners in the browser. The on and once methods allow us to register handlers/listeners to the _events object. Then, the emit method is used to call those listeners when that method is called. The important thing to note about the emit method is this can be used in other inherited classes (Function prototypes) to run that method internally when certain events happen to the object. Now, a small detail on the event architecture.

The reason event listeners are so popular is that they allow you to link objects together without tightly coupling them together. This is very important because it allows you to build your projects in a more modular way. Listening for events allows objects to send messages to each other along with the necessary parameters.



Anyway, I hope this helps you, and that you can use event listeners in your project to effectively manage objects and classes.
http://endlessillusoft.com/javascript-eventlisteners/
Thank you, it to bad your are not showing any useful context in the core rmmv..
Eventlistener gives me headaches ! :dizzy:
very easy use in html context but i n rmmv && pixi !!?
@Kino
Thanks for the helpful texts!
Just a small note, the text above the EventEmitter class is doubled.
Thanks for that I'll edit it when I get a chance. @Jonforum they're good for loose coupling all of MV-s sprites technically should have an on and once method.

The question is what do you want to use them for? After all, you can make any event you want.

I'll give a quick concept of a pattern for an event emitter. You want to create a link between your MV character walking over a region and a function in your code.

You can use a Broadcaster; a broadcaster emits events you create and you can set functions or objects to listen to that event and do something.

Code:
var Broadcaster = new PIXI.utils.EventEmitter;

Broadcaster.on("touchRegion", function(){ console.log("Touched Region"); });

Broadcaster.emit("touchedRegion");

The advantage of Code like this is that to add more stuff later on, you just add more event handlers. You don't need to do large amounts of restructuring of your code to support new features.
I followed a great yard, very simple, in html.
But when I applied these thecnique in rpg maker, nothing worked.

I have to be an idiot lol
In html I understand the principles but not in an application like rpg maker.
I add a complement of information. here is how to use them in rmmv, the better way i think.
PHP:
myFunction = function(event){
   console.log('your touche a key, check in', event);
};

 document.addEventListener('keydown', myFunction );
and also a another great example if need to bind a specific to a obj class.
PHP:
pixiSSA.DEBUG.onClick = function(event){
  this.myVar = 'abcd';
  this.data = event;
};
document.addEventListener('mousedown', pixiSSA.DEBUG.onClick.bind(pixiSSA.DEBUG) );

Original thread: https://forums.rpgmakerweb.com/threads/javascript-event-listeners.82892/