About private names

Sam Tobin-Hochstadt samth at ccs.neu.edu
Sat Mar 19 17:02:59 PDT 2011


On Sat, Mar 19, 2011 at 7:41 PM, Andrew Dupont <mozilla at andrewdupont.net> wrote:
>
> Or, to use another example: let's say `MyObj` was defined in a different scope, one in which `clone` was assumed to be a public name. Can I do this...
>
> private clone = installCloneLibrary();
> var twin = [{a:0}, {b:1}].clone();
> var thing = MyObj.clone();
>
> ... and have it work the way I intend? In other words, once ES5 fails to find something with the `clone` private name defined on `MyObj`, will it try to find the property with the _public_ name of `clone` before it gives up? (If I'm reading this right, it won't.)

You're correct -- this won't do what you probably intended.  But the
great thing about private names is that this is a problem you can
*locally* fix.  For example:

private myClone = installCloneLibrary();
var twin = [{a:0}, {b:1}].myClone();
var thing = MyObj.clone();

or

var cloneProp = installCloneLibrary();
var twin = [{a:0}, {b:1}][cloneProp]();
var thing = MyObj.clone();

will both do what you expect, without requiring changes to either
CloneLibrary or MyObj.  --
sam th
samth at ccs.neu.edu


More information about the es-discuss mailing list