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)}}`);