On 03/10/11 19:49, Erik Arvidsson wrote:
On Mon, Oct 3, 2011 at 14:50, Axel Rauschmayer<[email protected]> wrote:
It’s a performance thing support for dynamic "super" would slow everything
down and is thus not worth it. In the current spec, there is a method for
moving methods with "super" from one object to another. It’s not as elegant,
but given the performance cost of dynamic "super", it’s the right call.
I'm not sure it is just a performance issue. I believe the semantics
is wrong too.
class B {
a() {
this.b();
}
b() {
print('B.b');
}
}
class C extends B {
a() {
super.a();
}
b() {
print('C.b');
}
}
var c = new C;
c.a(); // Should print 'C.b'
With a dynamic super the above would lookup b in the wrong object
(B.prototype instead of C.prototype)
As I understand, dynamic super is different from dynamic this.
With dynamic super, you'd still get `C.b' in that call, because the
`thisObject' from the a function remains the same, super only changes
the object in which the method is looked upon.
So, a desugared (and wrong, I reckon, but bear with me for awhile) way
of writing that would be:
var C = {
a: function() {
B.a.call(this) }
b: function() {
print('C.b') }}
var c = Object.create(C)
c.a() // 'C.b'
The problem with that explicit `B' is that a function doesn't really
belong to an object in JS, it might be assigned to anything, and it's
expect that it'll work all the same. So, the engine would need a way of
tracking what the next object in the prototype chain is for *looking up*
the method to use. Once it finds that method, it applies the method to
the current `thisObject'.
The problem then, with dynamic super, becomes that one needs to pass the
called method a hint of what the next object in the prototype chain is,
since we can't use `this' — it'll always be the same in all methods
called through `super'.
Static super solves that problem by placing that information directly in
the method, so it's just a lookup away. Dynamic super solves that
problem by passing the information as an implicit parameter to *every*
function call, just like `this', thus being a perf problem.
I think it boils down to a performance vs semantics problem in the end.
I personally think the semantics dynamic super provides fit better with
a prototypical and overly dynamic language, as JavaScript is (at least
ES5 is).
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss