simple modules

Dave Herman dherman at mozilla.com
Sat Jan 30 16:12:14 PST 2010


> Ok thanks. I assume your longer description notes what the state
> would be as seen from two independent usages of module Counter?

I may not have made this clear enough in the strawman. A module is evaluated only once, so two independent usages will share state.

Now, this is "per context" -- the idea of our not-yet-written-nor-even-fully-conceived strawman on isolation would be that you could create a new Context in which modules could be independently instantiated. But within a given context, modules are never instantiated more than once.

> Also: Under your proposal, is it possible for the initial value of
> "counter" to be supplied by the client of the module?

Not directly; modules are not parameterized. If you want parameterization or multiple independent stateful values, you just use functions, objects and lexical scope, as usual:

module Counter {
    export function makeCounter(initial) {
        var state = initial;
        return {
            current: function() { return state },
            inc: function() { return state++ }
        };
    };
}

Dave


More information about the es-discuss mailing list