0

I'm just learning Javascript and some new features ES6 etc. With new ES6 style we can define two classless objects like this:

var parent = {
  foo() {
    console.log("Parent!");
  }
}
var child = {
  foo() {
    super.foo();
    console.log("Child!");
  } 
}
Object.setPrototypeOf(child, parent);

And the call 'super' is working as expected here. But, we also can define the same hierarchy in old style like this:

var parent2 = {
  foo: function () {
    console.log("Parent!");
  }
}
var child2 = {
  foo: function () {
    super.foor();     // <-- Error
    console.log("Child!");
  }
}

In this case call to 'super' produces the error "Uncaught SyntaxError: 'super' keyword unexpected here ". So here we can only completely override parent method and cannot call it. To me this behaviour looks absolutely inconsistent. Because both definitions looks same to me! Do I misunderstand something ?

str
  • 42,689
  • 17
  • 109
  • 127
  • 2
    Per MDN: [The super.prop and super\[expr\] expressions are valid in any method definition in both classes and object literals.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super) `foo() { … }` is defining a method, `foo: function() { … }` is defining a property that happens to be a function. (I’m assuming the missing prototype assignment and the misspelling of `foor` in the second snippet were unintentional.) – Aankhen Jul 21 '18 at 11:22
  • *But, we also can define the same hierarchy in old style like this* - this is not a hierarchy. `parent2` and `child2` objects are unrelated. – Estus Flask Jul 21 '18 at 11:44

0 Answers0