extracting namespace from a property

Brendan Eich brendan at mozilla.org
Wed Feb 28 14:44:31 PST 2007


On Feb 28, 2007, at 6:28 AM, Yuh-Ruey Chen wrote:

> But that does give me an alternative and much simpler idea:  
> Overload the
> Name constructor and function to accept a single parameter, such that
> Name(k) == Name(null, k). This would allow code like this:
>
> for (k in o)
>     print(Name(k).identifier);

Ok, good idea -- updating the spec now. It is unusual to have  
optional leading arguments, but it makes sense here, and it actually  
corresponds to E4X's QName:

js> qn = new QName('local')
local
js> qn.localName
local
js> qn.uri

js> qn2 = new QName('uri', 'local')
uri::local
js> qn2.uri
uri

> No need for a switch type statement, although that would probably be
> more efficient.

It would, yeah.

> Well as I mentioned, although Name would derive from String, it would
> still have to redefine all of String's methods.

Not so -- see ES1-3, where String.prototype methods except for  
toString and valueOf are intentionally generic. They convert their  
this parameter ToString and then operate on the result of that  
internal type conversion call.

> This implies that any
> method added to String's prototype would not automatically work with a
> Name instance. Unless the way Name delegates String methods via some
> __noSuchMethod__/__resolve__ hook.

Generic methods on String.prototype automatically work, where "work"  
is defined as operate on ToString(n), without hassle. If one adds a  
specific String.prototype method that can't cope with Name, but  
operates on its |this| parameter only if that parameter is exactly a  
string instance already, then it will throw when called on Name --  
which will tell the programmer to extend Name.prototype too (if we do  
decide to make class Name be dynamic).

>> Given Name <: String, it makes sense for (typeof n === "string")
>> given (n is Name).
>>
>
> Does this mean that in ES4 typeof Object(string) == "string"  
> instead of
> "object" (and for other primitive wrappers)?

That would have been the case if we had managed to merge string  
primitive type with String object wrapper, but we can't, so what  
happens instead is the same as today:

js> typeof Object('hi')
object
js> Object('hi') instanceof String
true

> Since Name instances are
> objects, typeof name should be "object", unless objects and their
> primitive counterparts have been unified with respect to typeof.

typeof new Name('hi') === "object", as you would expect.

We can't merge string and String, etc.

/be



More information about the Es4-discuss mailing list