Trying to refactor a simple function:
// arr - array of objects with functions
function eventNotify(arr, event) {
for (var i = 0; i < arr.length; i++) {
var a = arr[i];
if (typeof a[event] === 'function') {
a[event]();
}
}
}
into this one:
function eventNotify(arr, event) {
for (var i = 0; i < arr.length; i++) {
var a = arr[i][event];
if (typeof a === 'function') {
a();
}
}
}
I'm stuck trying to comprehend how such change manages to break all my tests.
How is it possible that the second implementation is functionally different from the first one?
I even tried to split the use of indexes, thinking that maybe it is treated as a 3D array:
var a = arr[i];
a = a[event];
But no, this makes no difference.
Please somebody point out what on earth am I changing in the logic of the algorithm there! I'm wracking my brain over this one now.
I'm testing it under Node.js 10.9
thiswon't be set when the function is called.thison explicit array addressing works, as opposed to just a variable. I knew the secret was somewhere in the intensity of my head-banging on the table... But this one is quite a revelation. If you want to make it into an answer, I will accept it ;)thisvalue) isarr[i]. In the second, its the global object.