News for hackers. Stuff that matters.
Re:Don't use functions as callbacks unless they're designed ... by giantDinosaur on Saturday February 06, 2021 @07:56AM (#26046207)
Here you go:

  Array.prototype.map = function (func) {
    if (func.length !== 3) {
        throw new Error(
            "bad! this function you've passed must take THREE arguments. Grr!"
        );
    }
    for (let i = 0; i < this.length; i++) {
        this[i] = func(this[i], i, this);
    }
    return this;
  };
;)

(making this workable and bug free is left to the reader or multi-billion dollar corporations)

There can be a non-indexed map as well.

I'm most curious as to who uses the last argument in the JS map function!

Re:Don't use functions as callbacks unless they're designed ... by dragonwriter on Sunday February 07, 2021 @06:50AM (#26054399)
> I'm most curious as to who uses the last argument in the JS map function!

It's convenient for some things that would otherwise require a reduce (but where reduce isn't particularly more efficient, because you just need lookahead/lookbehind) or an imperative loop, like transforming a list to a set of moving averages over the list.

It's a little more expressive than reduce our imperative lots loops in those cases, too.

Re:Don't use functions as callbacks unless they're designed ... by kuschku on Sunday February 07, 2021 @06:37AM (#26054327)
> I'm most curious as to who uses the last argument in the JS map function!

array.map((val, index, {length}) => if (index + 1 == length) { alert("last element") } else { alert("element #"+index) })