Earlier quoted context omitted.
Please do.
It won't alert quite what you want - when you click on each element in the list, it will alert "4". This is because the function assigned to the click event of each li is bound to the "i" variable used in the loop by a closure. Variable i is incremented to 4 before the loop ends, so that is what each will print. You could use make the function used in the click handler take a parameter, then use partial function appl…
JavaScript Interview Question
21–30 of 63 posts
Re: JavaScript Interview Question
#22I think they all alert '3' because of the missing 'var' keyword.
Just adding var wouldn't be enough, you're still referencing the variable from the outer scope. You need to create your own copy. Instead of: els[i].addEventListener('click', function(){alert(i);}); Do: els[i].addEventListener('click', function() { var j = i; alert(j); }); Edit: This is wrong, archgoon has the correct answer below. That'll teach me for jumping on closure problems in the morning :)
Edit: Oops, this is wrong as well. :-)
Re: JavaScript Interview Question
#23Spoiler Alert! each list item alerts 4 when clicked. This has to do with closures in js. The way I understood it (and please correct if inaccurate) is that each list item has an event listener that calls function: alert(i). However, it's not true that each list item has its own value for i. The value of i is figured out only when the link is clicked. Once script finishes, i has the value 4, and when clicked on, each…
Re: JavaScript Interview Question
#24I think they all alert '3' because of the missing 'var' keyword.
Just adding var wouldn't be enough, you're still referencing the variable from the outer scope. You need to create your own copy. Instead of: els[i].addEventListener('click', function(){alert(i);}); Do: els[i].addEventListener('click', function() { var j = i; alert(j); }); Edit: This is wrong, archgoon has the correct answer below. That'll teach me for jumping on closure problems in the morning :)
Re: JavaScript Interview Question
#25I think they all alert '3' because of the missing 'var' keyword.
Just adding var wouldn't be enough, you're still referencing the variable from the outer scope. You need to create your own copy. Instead of: els[i].addEventListener('click', function(){alert(i);}); Do: els[i].addEventListener('click', function() { var j = i; alert(j); }); Edit: This is wrong, archgoon has the correct answer below. That'll teach me for jumping on closure problems in the morning :)
You could make the function take a parameter, then return a function which uses that parameter, or use currying.
Edit: I've posted a working version in my comment further down, http://news.ycombinator.com/item?id=2124559
Re: JavaScript Interview Question
#26Re: JavaScript Interview Question
#27I think they all alert '3' because of the missing 'var' keyword.
Just adding var wouldn't be enough, you're still referencing the variable from the outer scope. You need to create your own copy. Instead of: els[i].addEventListener('click', function(){alert(i);}); Do: els[i].addEventListener('click', function() { var j = i; alert(j); }); Edit: This is wrong, archgoon has the correct answer below. That'll teach me for jumping on closure problems in the morning :)
Re: JavaScript Interview Question
#28Re: JavaScript Interview Question
#29is it cheating to just use a custom property? https://gist.github.com/788257
Re: JavaScript Interview Question
#30is it cheating to just use a custom property? https://gist.github.com/788257