Hello,
I am running through a youtube tutorial on making a game with canvas and he speaks about not needing for example (window).addEventListener when running.
Could someone explain this to me a bit more.
So far im thinking window is an object that has already been written and has properties such as addEventListener that i am then using to move my player. By not having window is it just a specific case with this object?
Cheers
Sambo
Not sure I understand the question.
The global object has properties/methods on it that are globally available. For example, you can do setTimeout
instead of window.setTimeout
.
You usually attach event listeners on the document or on an element. You won’t often use addEventListener
without it being explicitly attached to something, e.g. document.addEventListener
or someElement.addEventListener
.
target
is the click target and currentTarget
is the element the event listener is attached to. In an empty document, the code below should log body
and Window
when you click it.
addEventListener('click', (e) => {
console.log(e.target);
console.log(e.currentTarget);
console.log('clicked!');
});
1 Like