为什么[[HomeObject]]在方法的简写语法上有所不同?

编程入门 行业动态 更新时间:2024-10-13 14:24:25
本文介绍了为什么[[HomeObject]]在方法的简写语法上有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此问题源自

然后,到看看 MethodDefinition 是什么,你去

在第7步中,它调用 MakeMethod()。如果您转到

所以,你已经发现了 super 依赖 [[HomeObject]] 设置并仔细阅读规范,这是设置它的唯一方法。因此,对于允许 super ,它必须调用 MakeMethod()并且唯一的方法 MakeMethod()被调用是使用上述语法之一和属性的常规对象文字语法,如 propName:fn 不是一个他们。

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?

解决方案

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:

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

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.

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.

更多推荐

为什么[[HomeObject]]在方法的简写语法上有所不同?

本文发布于:2023-07-19 08:43:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简写   有所不同   语法   方法   HomeObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!