3 ms·
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
by _v7gu 3y 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.
- Tade0 3y agoNowadays, as someone writing a library, you can work around prototype pollution via Symbols and let your users modify prototypes on their terms.
- Drakim 3y agoWould that mean that instead of obj.pipeTo() I'd have to call obj[PIPE_TO] or something like that?
- Tade0 3y agoEssentially. 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.
- mlajtos 3y agoExactly right! https://github.com/mlajtos/es1995 https://github.com/mlajtos/es1995