While implementing map i noticed that in arrow notation there is type error this.forEach
is not a function while in case of using es5 function its running fine.
Array.prototype.map = function(projectionFunction) {
var results = [];
//Array.foreach is also error
this.forEach( (arrayItem) => {
results.push(projectionFunction(arrayItem));
});
return results;
};
var res = [5,10,15].map((x) => { return x + 2; });
console.log(res);
Not working
Array.prototype.map = (projectionFunction) => {
var results = [];
this.forEach( (arrayItem) => {
results.push(projectionFunction(arrayItem));
});
return results;
};
var res = [5,10,15].map((x) => { return x + 2; });
console.log(res);
Error
this.forEach( (arrayItem) => {
^
TypeError: this.forEach is not a function