0

I'm recreating Javascript's map function and i'm not understanding where these parameters are coming from:

(this[index], index, this)

I understand what this is the array, i understand this[index] is the value, and index is the number that keeps track of the index on iteration, but where is that all coming from when i'm only passing in:

x => x * 5

that's the part i'm confused about and havne't found a good explanation about it online.

Array.prototype.mymap = function(callback) {
    const resultArray = [];
    for (let index = 0; index < this.length; index++) {
        resultArray.push(callback(this[index], index, this));  // confused about these parameters
    }
    return resultArray;
}

const sample = [1,2,3];    
var output = sample.mymap(x => x * 5);    
console.log(output);
user1189352
  • 3,628
  • 12
  • 50
  • 90
  • That’s your callback. `x` is the item parameter – Daniel A. White Jul 11 '22 at 21:43
  • 1
    The value of `this` doesn't depend on what you pass as an argument. You're calling `sample.mymap(...)` so `this` is `sample`. – Quentin Jul 11 '22 at 21:44
  • @Quentin okay I get that but what about "this[index], index, this" why is it in that particular order and HOW is that translating to passing it as a parameter into "x => x * 5"? – user1189352 Jul 11 '22 at 22:14
  • "why is it in that particular order" — Because you're reimplementing `map` and that's what `map` is supposed to pass to the callback. – Quentin Jul 11 '22 at 22:14
  • @Quentin ah man I don't get it... like I understand what those values mean .. I understand what map does I use it all the time for work but I'm not getting that part. wish the mod didn't close this question.. thanks for trying – user1189352 Jul 11 '22 at 22:17
  • @user1189352 `this[index]` is passed into the `x` parameter. `index` and `this` are passed into parameters that your callback didn't declare. Also check the other duplicates I added – Bergi Jul 11 '22 at 22:28
  • 1
    It's a normal function call. If you have a function `function foo(a,b,c) {}` and you call it with `foo(1,2,3)` then `a=1, b=2, c=3`. It's much simpler than you might think. – Felix Kling Jul 11 '22 at 22:31
  • @Bergi "index and this are passed into parameters that your callback didn't declare" i guess that's the confusing part.... i'll check our the duplicates and see if i can make sense of it. thanks – user1189352 Jul 11 '22 at 22:37
  • @FelixKling i feel like that doesnt' make sense here though. how is "function callback(x) { return x * 5 }".. how does the "this[index], index, this" translate into that parameter x in that callback? i asked a senior dev just now and he doesn't know either.. i don't think it's as straight forward as u guys are making this to be – user1189352 Jul 11 '22 at 23:19
  • 1
    @user1189352 It works just the same. If you have `function func(x) {}` and call `func(1, 2, 3)` then `x` is `1` and the other two parameters are ignored. If you have an anonymous arrow function `x => x * 5` and you pass `this[index], index, this` to it, then `x` will have the value of `this[index]` and `index` and `this` as second and third parameters will be ignored. – Ivar Jul 11 '22 at 23:30
  • 1
    @Ivar OMG I GET IT. THANK YOU SO MUCH – user1189352 Jul 11 '22 at 23:37

0 Answers0