9.6.2 - 'this' or 'super' in a static method
Lars T Hansen
lth at acm.org
Mon Sep 3 04:41:04 PDT 2007
On 8/21/07, P T Withington <ptw at pobox.com> wrote:
> The es3 code is:
>
> function A () ...;
> A.zot = function zot () { ... this ... }
>
> function B () ...;
> B.prototype = new A();
> B.zot = A.zot;
>
> hence `this` is A in A.zot and B in B.zot.
Right...
> I was thinking the equivalent es4 would be:
>
> class A {
> static function zot () { ... this class ... };
> }
>
> class B extends A {
> static var zot = A.zot;
> }
Hm... more like
function the_zot() { ... this ... }
class A {
static const zot = the_zot;
}
class B extends A {
static const zot = A.zot; // or the_zot, obviously
}
I think.
> so I would need `this class` to be `B` in `B.zot`. Per your
> 'probably not', I guess that isn't going to work. Is there a better
> way? Or do I have to duplicate the code for zot in each subclass?
Using your code I think so. After all, A.zot() is closed in the
environment of its class, you can pass it around but that doesn't make
it B's method whether B extends A or not.
Using my code, "this" references A and B as necessary, but the
function is no longer a method.
Now I see what you mean. You would like "this class" to be
dynamically scoped like "this". Yet when we were talking earlier
about "this function" and "this generator", they are both statically
scoped entities -- unlike "this". And that's how I've been thinking
about "this class" -- as static. The motivation for "this function"
was to be able to reference the function enclosing the reference not
by name, but by some mechanism resilient to name change, and similar
for "this generator". So I figure, why not the class.
Taking this further, would not "this" in your class-static method
capture exactly what you want to capture (except that it's illegal
right now and probably used by mistake about 25% of the time?)
Well, that takes us full circle...
--lars
More information about the Es4-discuss
mailing list