Exporting Symbols
Rick Waldron
waldron.rick at gmail.com
Fri Oct 16 19:46:24 UTC 2015
On Fri, Oct 16, 2015 at 2:47 PM Dean Landolt <dean at deanlandolt.com> wrote:
> The symbol registry is a great way to map universal (not just global, but
> fully cross-realm) names (strings) to unique, distinct concepts (symbols).
> But as a flat string namespace has all the same kinds of issues symbols
> were introduced to solve. IIUC throwing `Math.random` strings into the
> symbol registry is pretty much equivalent to writing (non-enumerable)
> Math.random object properties on the global object, right?
>
Global object is not cross-realm. Symbol registry is cross realm, try this
var iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.contentWindow.Symbol.iterator === Symbol.iterator; // true, this
is in the Symbol registry.
iframe.contentWindow.Symbol === Symbol; // false
iframe.contentWindow.Array === Array; // false
Also, the OP is actually asking to give away the Symbol key:
"There are special features we want to expose if a module defines a
certain key. However, we’d like to not rely on certain names just because
there’s some chance they came up with it themselves. If it was a symbol
that we gave them access to, it would be impossible for this to be the
case, they would have had to grab the symbol from us:
var SpecialSymbol = require(“our-symbols”).SpecialSymbol;
export { whatever as SpecialSymbol }"
My example wasn't intended to show Francisco what I think he _should_ do,
just to answer his question based on his expressed parameters. If it were
me...
// module.js
let sym = Symbol();
// ...use my sym for special features...
export { sym as feature }
Seems sufficient? Maybe I've misunderstood the constraints.
Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151016/651c4fbc/attachment.html>
More information about the es-discuss
mailing list