aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/new.target/index.html
blob: 63fd2f1b8a86d4d60816b3d0dbd4b853dfb3efce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
title: new.target
slug: Web/JavaScript/Reference/Operators/new.target
tags:
  - Classes
  - ECMAScript 2015
  - JavaScript
  - Reference
translation_of: Web/JavaScript/Reference/Operators/new.target
---
<div>{{JSSidebar("Operators")}}</div>

<div><strong><code>new.target</code></strong>属性允许你检测函数或构造方法是否是通过<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/new">new</a>运算符被调用的。在通过<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/new">new</a>运算符被初始化的函数或构造方法中,<code>new.target</code>返回一个指向构造方法或函数的引用。在普通的函数调用中,<code>new.target</code> 的值是{{jsxref("undefined")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">new.target</pre>

<h2 id="描述">描述</h2>

<p><code>new.target</code>语法由一个关键字"<code>new</code>",一个点,和一个属性名"<font face="Consolas, Liberation Mono, Courier, monospace">target</font>"组成。通常"<code>new.</code>"<code></code>作用是提供属性访问的上下文,但这里"<code>new.</code>"其实不是一个真正的对象。不过在构造方法调用中,<code>new.target</code>指向被<code>new</code>调用的构造函数,所以"<code>new.</code>"成为了一个虚拟上下文。</p>

<p><code>new.target</code>属性适用于所有函数访问的元属性。在  <a href="http://www.javascripttutorial.net/es6/javascript-arrow-function/">arrow functions</a> 中,<code>new.target</code> 指向最近的外层函数的<code>new.target</code>(An arrow function expression does not have its own this, arguments, super , or new.target) 。</p>

<h2 id="示例">示例</h2>

<h3 id="函数调用中的_new.target">函数调用中的 new.target</h3>

<p>在普通的函数调用中(和作为构造函数来调用相对),<code>new.target</code>的值是{{jsxref("undefined")}}。这使得你可以检测一个函数是否是作为构造函数通过<a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/new">new</a>被调用的。</p>

<pre class="brush: js">function Foo() {
  if (!new.target) throw "Foo() must be called with new";
  console.log("Foo instantiated with new");
}

Foo(); // throws "Foo() must be called with new"
new Foo(); // logs "Foo instantiated with new"
</pre>

<h3 id="构造方法中的_new.target">构造方法中的 new.target</h3>

<p>在类的构造方法中,<code>new.target</code>指向直接被<code>new</code>执行的构造函数。并且当一个父类构造方法在子类构造方法中被调用时,情况与之相同。</p>

<pre class="brush: js">class A {
  constructor() {
    console.log(new.target.name);
  }
}

class B extends A { constructor() { super(); } }

var a = new A(); // logs "A"
var b = new B(); // logs "B"

class C { constructor() { console.log(new.target); } }
class D extends C { constructor() { super(); } }

var c = new C(); // logs class C{constructor(){console.log(new.target);}}
var d = new D(); // logs class D extends C{constructor(){super();}}
</pre>

<p class="summary">从上面类 C 和 D 的例子可以看出来,new.target 指向的是初始化类的类定义。比如当 D 通过 new 初始化的时候,打印出了 D 的类定义,C 的例子与之类似,打印出的是 C 的类定义。</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>

<p>{{Compat("javascript.operators.new_target")}}</p>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Functions">Functions</a></li>
 <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
 <li><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/new">new</a></code></li>
 <li><code><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/this">this</a></code></li>
</ul>