Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

javascript - Why [[HomeObject]] is different in shorthand syntax of method?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...