Are Static Members Inherited?
This has come up on this list before; please see the archive:
mail.mozilla.org/private/es4-discuss/2007-January/thread.html
(the "inheriting statics" thread).
Also, what section 9.2.7 do you mean? Could you cite the URL of the
document containing that section? Thanks,
On 8/17/07, Brendan Eich <brendan at mozilla.org> wrote:
This has come up on this list before; please see the archive:
mail.mozilla.org/private/es4-discuss/2007-January/thread.html (the "inheriting statics" thread).
I can't access that page, I'm not sure if I even have a pwd, but I found it here: www.nabble.com/inheriting-statics-tf3294493.html#a9164269
Also, what section 9.2.7 do you mean? Could you cite the URL of the document containing that section? Thanks,
I got the example from the PDF at the bottom of the page here: developer.mozilla.org/es4/spec/spec.html
"... Unlike in some other object oriented languages (e.g. Java), static properties of the base class are not inherited, but they are in scope in the static and instance methods of the derived class.
9.2.7 Scopes Static properties are in scope of bodies of static and instance methods of the same class. Instance properties are in scope of the bodies of the instance methods. Instance properties shadow static properties with the same name. Static properties of base classes are in scope of static and instance methods of a class. class A { static var ax } class B extends A { static var bx } class C extends B { static var cx var ix function m() { var mx gx = 10 ax = 20 bx = 30 cx = 40 mx = 50 } } var gx o = new C o.m() Scopes: • { mx } - activation scope • { ix } - instance scope • { cx } - static scope C • { bx } - static scope B • { ax } - static scope A • { gx } - global scope 40
Here's what happens when I try it in es4 reference implementation:
class A {static var ax = 10;} class B extends A { static var bx = 20;} print( new B().ax )
undefined
print( new A().ax) undefined
print(A.ax) 10
print(B.ax)
undefined
Is the PDF wrong?
Garrett
Static properties are in scope of bodies of static and instance methods of the same class. Instance properties are in scope of the bodies of the instance methods. Instance properties shadow static properties with the same name. Static properties of base classes are in scope of static and instance methods of a class.
class C extends A { function m() { print( ax ); } } new C().m()
20
Ah, I see, so the static properties of the superclass are visible in the scope chain, but not on the instance:
class D extends A { function md() { print( this.ax ); } } new D().md()
undefined
and not even through the subclass's constructor:
class E extends A { function me() { print (this.constructor.ax); } } new E().me();
Right?
The example in section 9.2.7 demonstrates static members, including those of an ancestor superclass, being accessible through an instance.
Section 9.6.2 clearly states that static methods are not accessible through derived class objects (though does not mention anything about static properties).
Am I to understand that static members (methods and properties) are visible through the scope chain of instances of derived classes (as in example in 9.2.7)?
Garrett