[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Anyone cares enough to explain this???

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

File: Jesus Christ.png (69KB, 1758x600px) Image search: [Google]
Jesus Christ.png
69KB, 1758x600px
Anyone cares enough to explain this???
>>
what do you mean by work? put it into a jsbin or how it's called
>>
>>59468693
var has function scope. when the event handler triggers it will always refer to squares-length - 1.

"this" refers to the element that triggered the handler.

if you replace the "var" in your for loop with a "let", the code on the right will work too.
>>
>>59468755
let me correct myself, "i" will be "squares.length" and hence the element you are retrieving from the array in the event handler will be undefined.
>>
Thanks for the help. So event handler should only work on the last element?
>>
>>59468890
no it wont work on any element because i will be out of bounds of the array.

you want to read about closures in javascript and about the difference between using "let" and "var" to declare variables.

replace "var" with "let" and your code will work.
>>
give up anon
>>
>>59469389
I will never give up!

>>59469164
Will do, thanks kind sir!
>>
>>59469676
>thanks kind sir!
If I knew you spoke like such a faggot I wouldn't have bothered to help you.
>>
>>59468755
If you want to take advantage of scoping you'd have to scope another variable in a smaller scope:

for( var i=0; i< squares.length; ++i) {
{
let scoped_i = i;
squares[i].addEventListener("click", function() {
//squares[scoped_i]; will work here
});
}
}
>>
you can just use let in the for loop.

for(let i=0; i< squares.length; ++i) {
{
squares[i].addEventListener("click", function() {
//squares[i]; will work here
});
}
}
Thread posts: 11
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.