es-membrane
es-membrane copied to clipboard
Dry and wet objects distinguishable by their prototype
If wrapping a (plain empty) object in a membrane, dry and wet objects have different prototypes. Besides being distinguishable through Object.getPrototypeOf
, they usually are logged as different strings. Example with the Node.js REPL:
> wet
{}
> dry
rv {}
Is there any option I can pass to the membrane constructor to smooth this difference, or is this inevitable?
If you want the membrane to be reasonably transparent, it is inevitable.
There might be some things you can do - such as providing an alternate toString() method - but I think at this stage of development, it's premature to put that into the Membrane library itself.
I'm willing to be persuaded otherwise.
Okay, I just did not know a new prototype was needed. Thanks for your replies.
(out of curiosity, since I am getting interested in proxies and membranes: what is it that cannot be done by the proxy handlers alone?)
That... is a very good question. One I don't have a clear answer to.
First off, the question should be rephrased: what can't a proxy or proxy handler do that an object can? JavaScript doesn't support operator overloading on objects, for example, nor on proxies. Which is a shame, in my opinion.
I think that proxies can do at least as much as objects can. More specifically, I haven't found anything that doesn't meet that criteria.
That said, I think JavaScript arrays specifically, and containers more generally, need to be carefully considered in the membrane model. For instance, if you simply hide the second element in an array through a membrane, the receiver of that array will not be pleased to have the look-up array[1] either return undefined or throw an error.
I hope to give a talk on this library in late July, at the TC39 meeting in Redmond, WA. If so, arrays will definitely be one of the points I raise, as an unsolved problem.