I've searched around, and I know there are tons of duplicate questions about solving this problem, but I know how to solve the problem and am not asking about that. I'm trying to better understand WHY this happens. I'm trying to understand based off of this explanation
var elems = document.getElementsByClassName("myClass"), i;
for (i = 0; i < elems.length; i++) {
elems[i].addEventListener("click", function () {
"use strict";
this.innerHTML = i;
});
}
I'm confused about how each functions retains a reference to the same copy of i because i is a number, which isn't passed around by reference, right? For example:
> a = 1
1
> b = a
1
> a = 2
2
> b
1
That being the case, I don't understand why each function is given a reference to a number since, unless I'm mistaken, numbers aren't passed around by reference. I've read explanations of this in several places but none of them have been able click for me or give me one of those 'Aha!' moments, so any help is much appreciated.