Lexically Scoped Object Extensions (was About private names)

Waldemar Horwat waldemar at google.com
Mon Mar 21 17:55:05 PDT 2011


On 03/21/11 17:42, Allen Wirfs-Brock wrote:
> On Mar 21, 2011, at 1:13 PM, Erik Arvidsson wrote:
>>
>> The above use case cannot be solved using private names because
>> private names conflict with public names.
>>
>
> Erik, I'm not sure that my understanding of the intended semantics of your extension statement is totally correct. But given what I I think you intended here is how I might imagine it desugaring using private names. Let me know what I misinterpretered:
>
> {
> private filter;
> Object.prototype.filter = function(fun) {
> var publicFilter= this["filter"];
> if (publicFilter) return publicFilter.apply(this,arguments);
> var retval = {};
> for (var key in this) {
> if (fun(this[key])
> retval[key] = this[key];
> }
> return retval;
> }
>
> function largerThanN(obj, n) {
> return obj.filter(function(item) {
> return item > n;
> }
> }
>
> var a = [0, 1, 2, 3, 4];
> print(largerThanN(a, 2));
> var o = {a: 0, b: 1, c: 2, d: 3, e: 4};
> print(largerThanN(0, 2));
>
> delete Object.prototype.filter
> }

You'd still run into all of the issues caused by "private filter" behaving like a C++ #define.  For example:

var foo = {filter: 34};
then pass foo to an outside client.

     Waldemar


More information about the es-discuss mailing list