-1

How would I overload the Set.prototype.add method, for example I'm trying to do:

// allow overloading the Set().method function by passing multiple arguments
Set.prototype.add = function(x) {
    for (let x of arguments) {
        super.add(x); // doesn't Set act like a class?
    }
    return this;
}
let s = new Set([1,2,3]);
s.add(4,5,6);
console.log("Set: ", s);
// 'super' keyword unexpected here

Or do I need to alias it like this so I can call the parent via Function.call/apply, such as:

Set.prototype.addMultiple = function(x) {
    for (let x of arguments) {
        Set.prototype.add.call(this, x);
    }
    return this;
}
let s = new Set([1,2,3]);
s.addMultiple(4,5,6);
console.log("Set: {" + Array.from(s) + "}");

Or, what about just aliasing the original method, is that considered ok? (or does Set.prototype._add = Set.prototype.add; have unintended side effects?):

Set.prototype._add = Set.prototype.add;
Set.prototype.add = function(x) {
    for (let x of arguments) {
        Set.prototype._add.call(this, x);
    }
    return this;
}

let s = new Set([1,2,3]);
s.add(4,5,6);
console.log(`Set: {${Array.from(s)}}`);
David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

1

You don't need all of these fancy features; just this:

Set.prototype.addMultiple = function(x) {
    for (let x of arguments) {
        this.add(x);
    }
    return this;
}

While we're at it, let's use const instead in the loop:

for (const x of arguments) {

And also arguments? That's some 2010 code. Nowadays we use spread/variadic arguments:

Set.prototype.addMultiple = function (...args) {
    for (const x of args) { ... }
     
    // rest omitted
}

Keep in mind that extending/modifying native prototypes is generally frowned upon, so you're better off extending the class instead.

kelsny
  • 23,009
  • 3
  • 19
  • 48
  • Thanks for this, that's very helpful. Noe question though: how does `this.add(x)` work, why doesn't that call itself and so produce an infinite loop? – David542 May 04 '22 at 04:36
  • Oh my god I'm sorry! `Set.prototype.add` => `Set.prototype.addMultiple` ‍♂️ – kelsny May 04 '22 at 04:49
0

You can subclass the Set with something like the following:

class _Set extends Set {
    add(_) { 
        Array.from(arguments).map((x) => super.add(x));
        return this;
    }
}

let s = new _Set([1,2,3]);
s.add(4,5,6);
console.log(`Set: ${Array.from(s)}`);
David542
  • 104,438
  • 178
  • 489
  • 842
  • 2
    Please do not use `.map()` for simple array iteration. Use `.forEach()` or a normal loop to do that. – VLAZ May 04 '22 at 05:15
  • @VLAZ ok, but what's wrong with using `.map()` (I'm a beginner to js). – David542 May 04 '22 at 05:21
  • 4
    [Is performing a mapping operation without using returned value an antipattern?](https://stackoverflow.com/q/56903693) – VLAZ May 04 '22 at 05:23