Implement map on a Prototype -- how to work it out without "this" or the global var "s"

*** 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:

Not really, and I’m not sure why you want to do this. Any hack you figure out has to use this in some way anyway, so anything you do is just misdirection. this is the calling context, so you’re asking how to refer to the calling context without referring to the calling context, which doesn’t really make sense.

You can just pass the callee into a function, ie how functional languages do things. You completely lose all the niceties of prototype methods, but it will work fine, although it in no way fulfils the criteria of the challenge:

function map(inputArray, callback) {
  var output = [];
  for (var i = 0; i < inputArray.length; i++) {
    output[i] = callback(inputArray[i]);
  return output;


> map([1, 2, 3, 4], function(item) { return item * 2; })
[2, 4, 6, 8]
1 Like

Tnx Dan!

I was just curious about the possibilities.