Need some clarification here

The solution was:

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
  var newArray = [];
  // Add your code below this line
  this.forEach(a => newArray.push(callback(a)));
  // Add your code above this line
  return newArray;

var new_s = s.myMap(function(item) {
  return item * 2;

The this keyword refers to whatever array the myMap method was used on correct?
And what’s really confusing is on the first line the word callback appears it is written as a parameter. But when it is used inside the myMap declaration, it seems to be used as a function with a passed as a parameter. So is callback a parameter or a function?


Yes. OK, I’m being a little silly. It is a function passed into the function as a parameter, so it is both.

It is a function passed into what function?

Array.prototype.myMap = function(callback) {

myMap is a function (or really “method”) that you are defining on the Array prototype. This method takes a single argument, a callback function.

But then where is the definition of the callback function, how do I know what it does?

var new_s = s.myMap(function(item) {
  return item * 2;

You are passing it into myMap when you call it.

callback = function(item) { return item * 2; }

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.