delegating to typed objects

Brendan Eich brendan at mozilla.org
Mon Jul 9 16:49:52 PDT 2007


On Jul 9, 2007, at 4:40 PM, Brendan Eich wrote:

>> And it has burned me that I could not do that.  I wanted to write an
>> 'annotated string' and couldn't because of exactly this restriction.
>> Will I be able to subclass built-in types?
>
> Subclass, yes -- provided the superclass is not final.

I should have added that String is not final in ES4 but string is.  
Yeah, we have had to add back primitive types -- they're wired deeply  
into the language. The pain should be less this time, though. And the  
final class string extends String, so you can decorate  
String.prototype with new function-valued properties and primitive  
strings can be operated on by those methods.

Similarly, boolean <: Boolean.

The current reference implementation makes {int,uint,double,decimal}  
<: Number, but we hope to change that while preserving backward  
compatibility, by making all of {int,uint,double,decimal,Number}  
peers and wiring up the prototype properties of the first four to the  
object denoted by Number.prototype. That's actually more compatible  
if you study it a bit. ES3 today (SpiderMonkey):

js> i = 42
js> i.__proto__ === (3.14).__proto__
true
js> i.__proto__ === Number.prototype

No extra layer of int.prototype between i.__proto__ and  
Number.prototype.

So we will make the same fix to boolean and string -- they won't have  
their own class prototype objects, rather they'll share their  
respective capitalized super-class's prototype. This means that  
boolean need not extend Boolean, nor string extend String, which will  
avoid some ugly bootstrapping magic.

People do use String and Number prototyping of primitive values  
(whether int or double), so compatibility is worth a special case  
here when setting up the built-in classes.

/be




More information about the Es4-discuss mailing list