How much can we skirt the lines here?
Assuming that we want the following line to work [1, 2, 3].sum();
then we can very easily just make it do something. Note that due to the automatic semicolon insertion rules, it's not necessary what you have there to be an array. It might be array access with comma operator in it.
({3: {sum: () => console.log(6)}}) //<-- object
[1,2,3].sum(); //<-- array access
Or to make it more clear, here is the equivalent code:
const obj = {
3: {
sum: () => console.log(6)
}
};
obj[3].sum(); //<-- array access
Since, I don't see a definition of what sum
should do, the above covers all the requirements listed - no protoype shenanigans, no extra properties.
OK, technically, sum
doesn't sum anything, but here is a workaround: define it like this
sum: (a, b) => a + b
Now, it's technically a function that sums two numbers. There is no requirement to sum the sequence 1, 2, 3
that appears before calling the sum
, after all.