inheriting statics

zwetan zwetan at gmail.com
Sun Jan 7 14:09:17 PST 2007


On 1/7/07, Brendan Eich <brendan at mozilla.org> wrote:
> On Jan 7, 2007, at 11:10 AM, zwetan wrote:
>
> > quoting the doc
> > "...
> > This property is static in that it is specific to the class or
> > function that you create. For example, if you create a class, the
> > value of the prototype property is shared by all instances of the
> > class and is accessible only as a class property. Instances of your
> > class cannot directly access the prototype property.
> > "
>
> This seems misleading to me, since the "value of the prototype
> property" is an object that is not shared as if it were a static
> property value. Instead, the properties of the static prototype
> property are visible in all instances, unless overridden
> ("shadowed").  So prototype is not an exception to the rule that
> static properties are not inherited by instances -- it's a different
> animal.
>

agreed, that's a little misleading from the doc, but as I said
it's one special case I could think of :).

Maybe it's because I'm too biased toward the way
ES3 is working, but not inheriting static make perfect sens to me.

I always saw ES3 closer to the objects,
and for statics I just consider them as slot attached to an object
and not part of the inheritance chain (either prototype or traits),
even if the object is a class definition it's still an object.



> Also, function prototypes are not "static" in any sense (the proposed
> Function.apply static method is a class static in ES4; Date.parse is
> a static method in ES1-4; but Function.prototype is the prototype of
> the Function class/constructor -- it's different from f1.prototype
> and f2.prototype for two functions f1 and f2). Function prototypes
> are "instance variables" of each particular function object.
>
> (I'm nit-picking the AS3 doc just so this doesn't get copied into the
> ES4 spec :-/.)
>

the AS3 doc is quite good imho, but still could be improved on
what someone may call "advanced subjects": the relation between
prototype chain and traits chain, the order of lookup for inheritance,
etc.
the kind of subject that if you dealt with ES3 and then start to learn ES4
should be ok to follow, but if you just start with ES4 could be hard
to fully understand.

tricky ex:
if you declare a property in the prototype, and redefine it from an instance,
does the shared property get redefined at the prototype level or
instance level ?

class Blah
    {
    prototype.shared = "hello world";

    function Blah() {}

    }

test1:Blah = new Blah();
test2:Blah = new Blah();
test1.shared = "bonjour le monde";
trace( test2.shared ); // ???

for someone knowing how prototype works in ES3 it can be easy,
but if you didn't dealt with object-based / prototype-based language before
hehe class ain't gonna help you to understand that ;)

and going on, now that you know that it is redefined at the instance level
how could you make it work at the prototype/shared level ?

class Blah
    {
    prototype.shared = "hello world";

    function Blah() {}

    public function change( value:String ):void
        {
        this.constructor.prototype.shared = value;
        }

    }

oh so that means that I can actually define a read-write property
shared by all my instances ?

class Blah
    {
    prototype.shared = "hello world";

    function Blah() {}

    public function set shared( value:String ):void
        {
        this.constructor.prototype.shared = value;
        }

    public function get shared():String
        {
        return prototype.shared;
        }

    }

test1:Blah = new Blah();
test2:Blah = new Blah();
trace( test1.shared ); //hello world
trace( test2.shared ); //hello world
test1.shared = "bonjour le monde";
trace( test2.shared ); // bonjour le monde

but wait that can also be done with a static

class Blah
    {
    private static var _shared:String = "hello world";

    function Blah() {}

    public function set shared( value:String ):void
        {
        _shared = value;
        }

    public function get shared():String
        {
        return _shared;
        }

    }

test1:Blah = new Blah();
test2:Blah = new Blah();
trace( test1.shared ); //hello world
trace( test2.shared ); //hello world
test1.shared = "bonjour le monde";
trace( test2.shared ); // bonjour le monde


sorry for the lengthy and boring examples,
but simply put about static and prototype and inheritance etc...

it should be really helpfull imho to start now to find real use case examples
that can show the real differences, why favour prototype over statics
for sharing
data between instances, or why not

I think people who gonna want to use ES4 and seeing only its class-based nature
will have question as "why still use the prototype, what can I do with
prototype that I can not do with static/class/etc. ?"

zwetan



More information about the Es4-discuss mailing list