This seems like a difficult thing to do because the prototype chain is only objects, so mutating them for the context of a single scope isn't easily done. A few cases come to mind as extremely confusing:
* If you override the prototype in a scope then pass the object to another function inside the block, does it stay overridden when passed? What if you _want_ to pass it as a normal array? Does that mean it needs to change the prototype back to default when calling the function, then change it back to your override again after? * What if something captures a reference to the object and then the block ends? Does every usage of that object in the captured scope then need to re-mutate the object then change it back again? If not, this means using this behavior in callbacks and such isn't possible. This approach encourages a huge amount of mutation, and it seems like it would be incredibly confusing for average users to have objects changing behavior out from under them. On Wed, Jul 5, 2017 at 11:24 AM, kdex <[email protected]> wrote: > One way to solve this might currently include extending `Array` and > overriding`indexOf` in a derived class to reflect the `Option` behavior > you're > after. > > On Wednesday, July 5, 2017 8:10:05 PM CEST Boris Cherny wrote: > > Hey guys, > > > > What would it take to get block scoped prototype extensions into > JavaScript? > > I’m curious to get some thoughts before I write a proposal. > > > > The use case is similar to Scala’s block scoped implicits. In my > application > > code I want Array.prototype.indexOf to return an Option<number>, rather > > than number | -1. If I patch Array’s prototype directly, it will break > > other libraries in my project. It would be nice to have a way to extend > the > > prototype for just a block, or just a file. > > > > Would a combination of block-scoped imports (maybe dynamic imports, to > patch > > the prototype) and some sort of onExitBlock hook (to unpatch the > prototype) > > be enough to implement this? Has anyone else thought about this sort of > > feature? > > > > Boris > > _______________________________________________ > > es-discuss mailing list > > [email protected] > > https://mail.mozilla.org/listinfo/es-discuss > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

