Lexically Scoped Object Extensions (was About private names)

Brendan Eich brendan at mozilla.com
Tue Mar 22 08:02:39 PDT 2011


On Mar 22, 2011, at 12:29 AM, François REMY wrote:

>> Sure, but the problem (as people keep pointing out) has two solutions: close the scope in which filfter binds to a private name, or use a renaming private declaration.
>> It's not perfect but there's no absolute inability to call public filter and private filter.
> 
> Adding "filter" to the Object property was done to have a fallback when the object you work on don't have a "filter" method built-in. In case the object you're working on has a "filter" method, it should be used. In this case, if you use private name, you can't have this behavior right (ie: write "myObj.filter()" and use the filter method implemented in myObj, if any, or the temporary extension to Object.prototype). It's because your intent was not to create a private name. It was to define a temporary extension to an object.

That's not what Erik's example shows. He simply wants to call the pre-existing "filter" (public name) method on an array in the same scope that he calls the extension "filter" (public or private, does it matter?) name added to Object.prototype somehow and temporarily.


> If you want this behavior right using private names, you'll need "if(myObj["filter"]) { myObj["filter"]() } else { myObj.filter(); }" or "(myObj["filter"]||myObj.filter).call(myObj)" which is really ugly. 

As noted, you can also rename private names or close scopes.

But now I do not understand the proposal in detail. How does the extension method get invoked? Dave's right, we need more details.

/be


More information about the es-discuss mailing list