什么是“new.target”? [英] What is "new.target"?

查看:241
本文介绍了什么是“new.target”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript 2015规范在 new.target 。 ecma-international.org/ecma-262/6.0/#sec-arrow-function-definitions-static-semantics-contains\">14.2.3

The ECMAScript 2015 specification mention the keyword (or words?) new.target exactly 3 times - 1 time in 14.2.3:


通常,Contains并不会在大多数函数表单中看到,但是
包含用于检测 new.target ,这个和$ b $中的超级用法b ArrowFunction。

Normally, Contains does not look inside most function forms However, Contains is used to detect new.target, this, and super usage within an ArrowFunction.

,并在 14.2.16


一个ArrowFunction不为参数定义本地绑定,超级,
this或 new.target 。对ArrowFunction中的arguments,super,this或
new.target 的引用必须解析为
词汇环境中的绑定

An ArrowFunction does not define local bindings for arguments, super, this, or new.target. Any reference to arguments, super, this, or new.target within an ArrowFunction must resolve to a binding in a lexically enclosing environment

MDN 提到它,但是非常模糊,页面不完整。

MDN mentions it, but is very vague and the page is incomplete.

Babel似乎不支持它。尝试在函数(箭头或其他)中使用 new.target 时,我收到语法错误。

Babel doesn't seem to support it. I got syntax errors when trying to use new.target in a function (arrow or others).

它是怎么回事应该使用?

What is it, and how is it supposed to be used?

推荐答案

你没有在规范中找到它,因为在语法定义中用空格写,作为新。目标。表达式的名称是 NewTarget ,你会发现这个词几次。

You didn't find it in the spec because in the syntax definitions it is written with blanks, as new . target. The name of the expression is NewTarget, and you'll find that term a few times around.

NewTarget 是第一个所谓的 元属性 ,可以在§12.3.18中找到。

NewTarget is the first of the so-called meta properties and can be found in §12.3.18.

其唯一目的是检索 [[NewTarget]] 当前(非箭头)功能环境的值。一个函数被调用时设置的值(非常像这个绑定),根据§8.1.1.3功能环境记录

Its sole purpose is to retrieve the current value of the [[NewTarget]] value of the current (non-arrow) function environment. It is a value that is set when a function is called (very much like the this binding), and according to §8.1.1.3 Function Environment Records:


如果此环境记录是由 [[Construct]] 内部方法创建的,则 [[NewTarget]] [[Construct]] newTarget 参数的值。否则,它的值为 undefined

If this Environment Record was created by the [[Construct]] internal method, [[NewTarget]] is the value of the [[Construct]] newTarget parameter. Otherwise, its value is undefined.

所以,一件事,终于使我们能够检测一个函数是否被调用为构造函数。

So, for one thing, finally enables us to detect whether a function was called as a constructor or not.

但这不是它的真正目的。那么那又是什么呢?这是ES6类不仅仅是语法糖的方式的一部分,以及它们如何允许我们继承内建对象。当您通过 new X 调用构造函数时,值尚未初始化 - 在输入构造函数体时尚未创建对象。它在 super()调用期间由超级构造函数创建(这在内部插槽应该被创建时是必需的)。不过,实例应该继承自最初调用的构造函数的 .prototype ,这就是 。它保存在 super()调用期间收到调用的最外层构造函数。你可以在规范中一直遵循它,但基本上它一直是 newTarget 不是当前执行的构造函数,它被传递到 OrdinaryCreateFromConstructor 程序 - 例如在§的第5步9.2.2

But that's not its real purpose. So what is it then? It is part of the way how ES6 classes are not only syntactic sugar, and how they allow us inheriting from builtin objects. When you call a class constructor via new X, the this value is not yet initialised - the object is not yet created when the constructor body is entered. It does get created by the super constructor during the super() call (which is necessary when internal slots are supposed to be created). Still, the instance should inherit from the .prototype of the originally called constructor, and that's where newTarget comes into the play. It does hold the "outermost" constructor that received the new call during super() invocations. You can follow it all the way down in the spec, but basically it always is the newTarget not the currently executed constructor that does get passed into the OrdinaryCreateFromConstructor procedure - for example in step 5 of §9.2.2 [[Construct]] for user-defined functions.

长文本,也许是一个比较适合的例子:

Long text, maybe an example is better suited:

class Parent {
    constructor() {
        // implicit (from the `super` call)
        //    new.target = Child;
        // implicit (because `Parent` doesn't extend anything):
        //    this = Object.create(new.target.prototype);
        console.log(new.target) // Child!
    }
}
class Child extends Parent {
    constructor() {
        // `this` is uninitialised (and would throw if accessed)
        // implicit (from the `new` call):
        //    new.target = Child 
        super(); // this = Reflect.construct(Parent, [], new.target);
        console.log(this);
    }
}
new Child;

这篇关于什么是“new.target”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆