*** My question ***
For an acceptable approach, we cannot simply use “s.length”, but “this.length” instead, so myMap could be used which any “caller” and not only the global “s”. It is understandable that the keyword “this” represents the “caller” here, the invocation context for the given. So, “this” would be the Array object “s”, where “s” invokes/calls for “myMap()”.
So far so good… (for me at least). What I want to know is if there are any other methods to find the caller, to find the object standing right before the dot in the invocation context, other than the keyword “this”.
*** My solution in which my question as based on ***
// the global Array
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
for (var i = 0; i < this.length; i++) {
newArray[i] = callback(this[i]);
}
// Add your code above this line
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype