The `super` keyword doesn't work as it should?

/#!/JoePea joe at trusktr.io
Mon Jul 18 21:00:26 UTC 2016


For example, both of these examples don't work in Chrome:

```js
function A() {
    console.log('A')
}
A.prototype.constructor = A
A.prototype.hello = function() {
    return 'hello'
}

function B() {
    console.log('B')
    // super()
    A.call(this)
}
B.prototype = Object.create(A.prototype)
B.prototype.constructor = B
B.prototype.hello = function() {
    return super.hello() + 'there'
}

new B
```

and

```js
let obj1 = {
    hello() {
        return 'hello'
    },
    sayHello() {
        console.log(this.hello())
    }
}

console.log('Obj1 says hello:')
obj1.sayHello()

let obj2 = Object.create(obj1)
Object.assign(obj2, {
    hello() {
        return super.hello() + 'there.'
    }
})

console.log('Obj2 says hello:')
obj2.sayHello() // Error
```

I was hoping `super` was more flexible than that.

For example, in the first snippet, why can't `super` simply be a shortcut
for "look up the prototype of the object that the method is called on, then
find the `.constructor` property and call it on `this`"? That seems to be
simple. It could throw an error if `.constructor` is not found, in the case
of ES5-style classes that aren't defined using that pattern that ES6
classes are syntax sugar for.

And in the second example, why does `super.hello` not work as expected?

I believe that these limitations may severely limit my ability to create
the multiple-inheritance tool that I'm imagining over at
https://esdiscuss.org/topic/symbol-for-modifying-property-lookup#content-8.

Any ideas or suggestions?


*/#!/*JoePea
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160718/e284695e/attachment.html>


More information about the es-discuss mailing list