简体   繁体   中英

Javascript: Calling a function with argument “e”

Here is the relevant piece of code:

window.addEventListener('mousemove', function (e) {
        myGameArea.x = e.pageX;
        myGameArea.y = e.pageY;
    })

What I don't understand is what the argument "e" is doing and how it is relevant, or what it's value is. If anyone needs me to link the entire piece of code ("it's a relatively small piece of code, one that runs a movable object"). I know that the question may not be well presented or that the answer may be obvious but I can't wrap my head around it well enough to phrase my question better. Also, google was no help. Thanks in advance!

EDIT

Some more examples from the code:

window.addEventListener('keydown', function (e) {
        myGameArea.keys = (myGameArea.keys || []);
        myGameArea.keys[e.keyCode] = true;
    })

and

window.addEventListener('keyup', function (e) {
        myGameArea.keys[e.keyCode] = false; 
    })

The addEventListener method takes two parameters. One is the event name. The second is a callback function. You're actually passing an entire function to addEventListener that will be called at a later time.

addEventListener will hold on to that method. When an event is triggered, addEventListener will call your callback method. When your callback is called, the caller will pass an object representing the event. It allows you to get the details of the event inside your callback method.

It is the event object. A parameter implicitly passed into the event listener function so that you can grab properties from the event that occurred. For example, the x,y coordinates of the mouse, the preventDefault method, the event target origin, etc.

See the documentation for more details on what the event object has, and how it's bound to the function event.

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