In JavaScript, this
depends on the function call method.
Calling General Functions
Method Call
Call constructor function
arrow function call, etc.
but in this case
class Numbers {
numberArray = [];
multiply(arr) {
arr.forEach(function(item) {
this.numberArray.push(item * item);
});
}
}
const numbers = new Numbers();
numbers.multiply([1, 2, 3]);
If you look at the fourth line in this class example,
Since arr.forEach
is calling a callback function with this
, I think the arr
is a this
but Why does this
means undefined
?
I don't know why it's a general function call.