Testing some code I found this problem, when I call the functions of the class Results directly, those work fine, but when I push those to an array and then I call the functions those don't work, because the this keyword is lost and the console shows the message of undefined.
class Results
{
sum(numbers){
let result=numbers.reduce((a,b)=>a+b);
return result;
}
state(numbers){
console.log(this);
let result= this.sum(numbers)>0 ? "positive" : "negative";
return result;
}
}
function play(){
let results=new Results();
let numbers=[1,2,3,4];
console.log(results.sum(numbers));
console.log(results.state(numbers));
let arrayfunctions=[];
arrayfunctions.push(results.sum);
arrayfunctions.push(results.state);
arrayfunctions.forEach(funcion=>
{
console.log(funcion(numbers))
});
}