extracting namespace from a property

Brendan Eich brendan at mozilla.org
Mon Feb 5 14:33:20 PST 2007


You are right on top of this one. I've proposed a Name object (Jeff  
suggested not using E4X's QName class) to reflect qualified  
identifiers. This is necessary because what you propose is ambiguous  
with respect to existing objects having properties with "::" in their  
names (which has always been allowed; these would be accessed via []  
of course).

Here's the guts of the proposal:

Problem Statement

The for-in loop combined with namespace-qualified names means we need  
a Name class reflecting namespace-qualified identifiers of enumerable  
properties:

let o = {x:1, 2:"two", '@funny':3};
for (let i in o)
   print(i, typeof i);

Per ES1-3, and required for backward compatibility, this script  
should print

x string
2 string
@funny string

Consider this code:

namespace n;
let p = {n::m: 42};
for (let i in p)
   print(i, typeof i);

It would be impossible to index p.n::m by p[i] if i were “m” (if the  
output of this loop were m string). We need something like the QName  
class from E4X (ECMA-357). Jeff proposes we call it Name.
Proposed Solution

The Name class is defined as follows:

class Name {
   function Name(ns : Namespace, id : string)
     : qualifier = ns
     , identifier = id
   {
   }


   intrinsic static function invoke(ns : Namespace, id : string) : Name
     new Name(ns, id);

   public const qualifier  : Namespace,
                identifier : string;
}

An enumerating for-in loop or comprehension must reflect an  
enumerable property name as a Name instance if the property was  
defined with an explicit or default namespace other than public.  
Otherwise, with no or a public namespace, enumerating for-in reflects  
the name as a string per ES1-3 and existing JS implementations.

A property access of the form o[n] where n is of type Name looks for  
a binding in the namespace n.qualifier whose unqualified name is  
n.identifier.

----

The only detailing that remains is what Name.prototype.toString  
should do. Current thinking is something like "[namespace public  
n]::m" for the example in the Problem Statement. Comments welcome.

/be



More information about the Es4-discuss mailing list