aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/new.target
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/operators/new.target
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators/new.target')
-rw-r--r--files/zh-cn/web/javascript/reference/operators/new.target/index.html99
1 files changed, 99 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/new.target/index.html b/files/zh-cn/web/javascript/reference/operators/new.target/index.html
new file mode 100644
index 0000000000..7d1c41ec89
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/operators/new.target/index.html
@@ -0,0 +1,99 @@
+---
+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>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<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>