mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-18 00:44:36 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -2,24 +2,24 @@
|
||||
<div></div>
|
||||
<div><strong><code>new.target</code></strong>属性允许你检测函数或构造方法是否是通过<a href="Reference/Operators/new">new</a>运算符被调用的。在通过<a href="Reference/Operators/new">new</a>运算符被初始化的函数或构造方法中,<code>new.target</code>返回一个指向构造方法或函数的引用。在普通的函数调用中,<code>new.target</code> 的值是<a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>。</div>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox">new.target</pre>
|
||||
<pre><code class="language-javascript">new.target</code></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 class="external" href="http://www.javascripttutorial.net/es6/javascript-arrow-function/" rel="noopener">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>的值是<a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>。这使得你可以检测一个函数是否是作为构造函数通过<a href="Reference/Operators/new">new</a>被调用的。</p>
|
||||
<pre class="brush: js">function Foo() {
|
||||
<pre><code class="language-javascript">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>
|
||||
</code></pre>
|
||||
<h3 id="构造方法中的_new.target">构造方法中的 new.target</h3>
|
||||
<p>在类的构造方法中,<code>new.target</code>指向直接被<code>new</code>执行的构造函数。并且当一个父类构造方法在子类构造方法中被调用时,情况与之相同。</p>
|
||||
<pre class="brush: js">class A {
|
||||
<pre><code class="language-javascript">class A {
|
||||
constructor() {
|
||||
console.log(new.target.name);
|
||||
}
|
||||
@@ -35,7 +35,7 @@ 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>
|
||||
</code></pre>
|
||||
<p class="summary">从上面类 C 和 D 的例子可以看出来,new.target 指向的是初始化类的类定义。比如当 D 通过 new 初始化的时候,打印出了 D 的类定义,C 的例子与之类似,打印出的是 C 的类定义。</p>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
|
||||
Reference in New Issue
Block a user