jeudi 21 décembre 2017

Javascript Closure scope return function

I'm currently looking at the 2nd solution of the 2nd question on this page,

http://ift.tt/2bAh4dU

I'm stuck why you need a closure.

function handlerWrapper(i) {
   return function() {
      console.log('You clicked element #' + i);
   }
}

Why do I need to the return function. When I try

function handlerWrapper(i) {
   
     console.log('You clicked element #' + i);
   
}

It doesnt work. What exactly does including the

console.log('You clicked element #' + i);

within the return function do? Why does it work and why do I need to do it within the return function?

Thank you ahead of time




Aucun commentaire:

Enregistrer un commentaire