Jordan Davis
Jordan Davis

Reputation: 1520

Set event listener with ES6 arrow functions

Question: Why isn't the event listener being set?

//ADDING THE EVENT LISTENER
document.addEventListener('DOMContentLoaded', init);

//DELCARING INIT, PASSING BLANK PARAM, STATEMENT
var init = () => console.log('Is Firing');

Upvotes: 1

Views: 3796

Answers (2)

ramin bakhshpour
ramin bakhshpour

Reputation: 79

init is a function expression , which means hoisting doesn't happen for it, so you should pull the function expression up before the listener binding. I suggest you study the function hoisting. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function#Function_declaration_hoisting

Upvotes: 0

void
void

Reputation: 36703

Your init should be declared first before using it.

//DELCARING INIT, PASSING BLANK PARAM, STATEMENT
var init = () => alert('Is Firing');

//ADDING THE EVENT LISTENER
document.addEventListener('DOMContentLoaded', init);

Upvotes: 4

Related Questions