Hacker News new | ask | show | jobs
by _v7gu 994 days ago
Honestly, if prototype pollution was not a problem for optimization and other things I’d just toss a

    Object.prototype.pipeTo = function(f) {
      return f(this)
    };
And call it a day. It does about 80% of what a pipe macro would do, has instant compatibility with other prototype methods and leads to very simple types. The only issue is that you have to define lambdas to call multivariable functions. D has got this very, very right.
2 comments

Nowadays, as someone writing a library, you can work around prototype pollution via Symbols and let your users modify prototypes on their terms.
Would that mean that instead of obj.pipeTo() I'd have to call obj[PIPE_TO] or something like that?
Essentially. And then have:

  Object.prototype.pipeTo = Object.prototype[PIPE_TO]
or some other name instead of `pipeTo` should there be a conflict.

JS already has a few well-known symbols like Symbol.toPrimitive that allow one to modify an object's behaviour (in this case what happens when `valueOf` or `toString` is called), so there's precedent.