This version of my es6 function doesn't work:
Array.prototype.concatAll = () => {
let results = [];
this.forEach((subArray) => {
subArray.forEach((item) => {
results.push(item);
});
});
return results;
};
When I use it like this:
var stocks = exchanges.concatAll();
The console says: Cannot read property 'forEach' of undefined
However this es5 version works just fine:
Array.prototype.concatAll = function() {
let results = [];
this.forEach((subArray) => {
subArray.forEach((item) => {
results.push(item);
});
});
return results;
};
Why is this? What exactly is happening with this
inside the es6 version? I would like to understand.