4

This question is derived from super keyword unexpected here

The accepted answer says:

Because super is only valid inside methods.

But in MDN, it seems these two are both methods:

let person = {
    greeting() {
        return "Hello";
    }
};

let friend = {
    // shorter syntax for method?
    greeting() {
        return super.greeting() + ", hi!";
    }

//  method?
//  greeting: function() {
//      return super.greeting() + ", hi!"; // Throw error: Uncaught SyntaxError: 'super' keyword unexpected here
//  }

};

Object.setPrototypeOf(friend, person);
console.log(friend.greeting());   

In understanding es6, Nacholas says:

Attempting to use super outside of concise methods results in a syntax error

Methods were just object properties that contained functions instead of data.

Any reference to super uses the [[HomeObject]] to determine what to do. The first step is to call Object.getPrototypeOf() on the [[HomeObject]] to retrieve a reference to the prototype. Then, the prototype is searched for a function with the same name. Last, the this binding is set and the method is called.

So it seems [[HomeObject]] is different in shorthand syntax of method? I'm curious why?

Community
  • 1
  • 1
Tina Chen
  • 2,010
  • 5
  • 41
  • 73
  • Can you show the containing context for your two code blocks? What are these inside of? `greet: function() {}` that's just a property declaration on a normal object is probably not technically a "method" as far as `super` is concerned. It's just a plain property. In all cases, why not just use the `class` syntax and avoid the whole issue. You're already requiring `super` to be available so it's not like you can't count on `class`. – jfriend00 Jul 04 '18 at 02:40
  • Thanks @jfriend00, 1. code blocks has completed. 2. in MDN, `greet: function() {}` seems satisfy `method` definition? That's what confusing me. 3. `class` syntax is another topic, I am just watching the book and curious about `super` behavior. – Tina Chen Jul 04 '18 at 02:59
  • Well, `super()` is designed for use with `class`. If that's where you use it, then you have none of these puzzles to figure out. – jfriend00 Jul 04 '18 at 03:53
  • In [MDN super #"Using super.prop in object literals"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super), there is also an example of using `super` in the object initializer / literal notation – Tina Chen Jul 04 '18 at 05:45
  • And notice that those object literal examples ONLY use the shortcut method syntax. It appears there's something special about that syntax. – jfriend00 Jul 04 '18 at 05:52
  • @jfriend00 Yes, that's what I want to ask: what's special about this syntax??This is not documented. BTW, I'll avoid to use `super` in object literal, thanks. – Tina Chen Jul 04 '18 at 05:54
  • 1
    I don't have the energy right now to try to follow the complex web of syntax in the EMCASCript specification, but it is the [`MakeMethod()` call](https://www.ecma-international.org/ecma-262/6.0/#sec-runtime-semantics-definemethod) that sets `[[HomeObject]]` and then allows the use of `super`. You'd have to follow the chain of syntax definitions to find out exactly what syntax does and doesn't lead to `MakeMethod()` getting called or not called. MDN is not the documentation source - the EMCAScript specification is the source. – jfriend00 Jul 04 '18 at 06:20
  • 1
    It looks like it's here: https://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer. Only the shortcut syntax is a [MethodDefinition](https://www.ecma-international.org/ecma-262/6.0/#sec-method-definitions) (which has super capabilities). The regular property syntax is just a property, not a "method" in the way that Javascript defines and initializes what it calls a "method". – jfriend00 Jul 04 '18 at 06:31

2 Answers2

10

First off, MDN is not official Javascript documentation. While it's often helpful, it's not the definitive source for anything related to the language. That official specification would be in the ECMAScript specification. That's where the Javascript grammar is defined.

In that document, there is something called a MethodDefinition. There are several syntaxes that can be used for a method definition. The greeting() {} syntax is one such syntax that can be used for a MethodDefinition. The typical object literal property definition of propName: function() {} is not. Here's how it is defined:

enter image description here

Then, to see what a MethodDefinition is, you go to section 14.3.8 where it documents the steps for a MethodDefinition as follows:

enter image description here

In Step 7, it calls MakeMethod(). If you go to that part of the specification, you will see that's where the [[HomeObject]] value gets set.

enter image description here

So, as you've already discovered super relies on [[HomeObject]] being set and perusing the specification, this is the only way that it gets set. So, for super to be allowed, it has to call MakeMethod() and the only way MakeMethod() gets called is with one of the above syntaxes and a regular object literal syntax for a property such as propName: fn is not one of them.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • This is so interesting for me if function declared not like method like property have value function which process happened. Which part explain this process in spessification. – Murad Sofiyev Sep 26 '19 at 02:41
  • @MuradSofiyev - I don't understand what you mean. – jfriend00 Sep 26 '19 at 02:57
0

The [[HomeObject]] property is defined on methods of both classes and regular objects. But for objects, methods must be declared exactly as method(), not "method: function()".

There are no differences for us, but they are for JavaScript.

The example below uses the not-method, property-function syntax. So it doesn't have [[HomeObject]], and inheritance doesn't work:

 let animal = {
  eat: function(){ 
    console.log(this)
  }
};

let rabbit = {
  __proto__: animal,
  eat: function() {
    super.eat();
  }
};

rabbit.eat(); 

// Error calling super (because there is no [[HomeObject]])

as mentioned above

the [[HomeObject]] value is set in the MakeMethod() method, in turn, is only available for functions that fit the role of the method, i.e. functions declared as:

PropertyName ( StrictFormalParameters ) { FunctionBody } that part of the specification