[PROPOSAL] "use" keyword

Rick Waldron waldron.rick at gmail.com
Fri Jul 25 13:35:13 PDT 2014


On Fri, Jul 25, 2014 at 4:29 PM, Tab Atkins Jr. <jackalmage at gmail.com>
wrote:

> On Fri, Jul 25, 2014 at 12:56 PM, Michaël Rouges
> <michael.rouges at gmail.com> wrote:
> > Hi David.
> >
> > Sure... I make a library that that needs to share a private variable with
> > all methods
> > of an instance passed by arguments on my lib' method.
> >
> > ```
> > var lib;
> >
> > (function () {
> >     var shared;
> >
> >     shared = 'a shared value';
> >
> >     lib = {
> >         method: function method(foreignObject) {
> >             var property;
> >
> >             for (property in foreignObject) {
> >                 if (foreignObject.hasOwnProperty('property') {
> >                     if (typeof foreignObject === 'function') {
> >                         // how to pass the "shared" variable
> >                         // preserving the inside this in foreignObject
> > methods
> >                         // and without to touch the methods args?
> >                     }
> >                 }
> >             }
> >         }
> >     };
> > }());```
>
> Ah, so you've got a "private" variable (declared in a closure) that
> you want to share with other objects which aren't created in the same
> closure.
>
> This is what Symbols/WeakMaps were made for.


I'd be interested to see what your approach would be—afaict, this would
require exposing the Symbol or WeakMap to whatever scope the foreign object
is defined in and then there's no point in using either.

Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140725/a8a3cdf5/attachment.html>


More information about the es-discuss mailing list