I have this key in an JavaScript object {}
resolve: function () {
var result = this.initialValue;
console.log('initial value:',result); // 5
this.functions.forEach(function (element, index) {
console.log('index:', index, 'result:',result); //index=0, result=undefined :(
var result = element.func(result);
});
}
result is defined outside the loop (with a value of Number(5)). But upon the first iteration of the loop, the result variable becomes undefined. Is there something about JS that I don't know?
Is the var result = element.func(result);
call somehow redefining result
in a weird way? No, that can't be, because that call comes after the first logging of result
.
in this case element.func() is simply a variable representing console.log()
so element.func(result)
should be equivalent to console.log(result)
, but it's printing out undefined instead of 5.
No idea what's going on.