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

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


Regarding the second example, [this resource](
http://exploringjs.com/es6/ch_oop-besides-classes.html#_caveat-objectassign-doesnt-work-well-for-moving-methods)
explains why `Object.assign` doesn't work as expected.

I believe the VM should be smarter than that, and let end-developers use
any technique for inheritance that they wish.

For example,maybe the VM should determine at runtime what the
`[[HomeObject]]` is based on what object the method is called on. Seems
like that would make `super` much more useful.

Doesn't that make more sense? If not, why not?

If this were the case, then the first example would work just fine too.

*/#!/*JoePea

On Mon, Jul 18, 2016 at 2:00 PM, /#!/JoePea <joe at trusktr.io> wrote:

> 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/5153acf6/attachment.html>


More information about the es-discuss mailing list