Private names use cases
Garrett Smith
dhtmlkitchen at gmail.com
Mon Dec 20 23:28:15 PST 2010
On 12/20/10, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> I've seen mentions in the recent thread that the goal of the "Private Names"
> proposal was to support "private fields" for objects. While that may be a
> goal of some participants in the discussion, it is not what I would state as
> the goal.
>
> I have two specific use cases in mind for "private names":
> 1) Allow JavaScript programmers, who choose to do so, to manage the direct
> accessibly of object properties. This may mean limiting access to methods
> of a particular instance, or to methods of the same "class", or to various
> friends or cohorts, etc.
If private is based on lexical scope then it's already possible but it
can be a bad smell.
For example:
var Blah = function() {
var privateBlah = {
complicatedDirtyWork : function() { alert("blah.") }
};
var Blah = {
goBang : function() {
privateBlah.complicatedDirtyWork();
}
};
return Blah;
}();
Blah.goBang()
Great for a one-off, but when you wanna have many of the ADT (many
instances of Blah), then there's an equal number of privateBlah and
the strategy must make some sort of lookup, e.g.
var delegateBlah = getPrivateBlah( this );
delegateBlah.doTrick();
That's a bit more work and when there are only a couple of fields, it
seems like too much trouble.
If it were authored in a way that obviates that cruft but could still
be facilitated by using scope tricks, as above?
--
Garrett
More information about the es-discuss
mailing list