blob: b00f08cb4e0d0f23af412c564452b6af0e7bae13 (
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
---
title: new.target
slug: Web/JavaScript/Reference/Operators/new.target
tags:
- Classes
- ECMAScript 2015
- JavaScript
- Language feature
- Reference
translation_of: Web/JavaScript/Reference/Operators/new.target
---
<div>{{JSSidebar("Operators")}}</div>
<p><strong><code>new.target</code></strong> プロパティを使用すると、関数やコンストラクタが <a href="/ja/docs/Web/JavaScript/Reference/Operators/new">new</a> 演算子を使用して呼び出されたかどうかを検出できます。コンストラクタや関数内で、<a href="/ja/docs/Web/JavaScript/Reference/Operators/new">new</a> 演算子でインスタンス生成すると、<code>new.target</code> はコンストラクタや関数への参照を返します。通常の関数呼び出しの場合、<code>new.target</code> は {{jsxref("undefined")}} です。</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-newtarget.html")}}</div>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate">new.target</pre>
<h2 id="Description" name="Description">概要</h2>
<p><code>new.target</code> の構文は、<code>new</code> キーワードとドット、プロパティ名 <code>target</code> で構成されています。通常、ドットの左側はプロパティアクセスが行われるオブジェクトですが、ここでの <code>new</code> はオブジェクトではありません。</p>
<p><code>new.target</code> プロパティは、すべての関数で使用できる疑似的なプロパティです。</p>
<p>クラスのコンストラクタでは、構築されたクラスを参照します。</p>
<p>通常の関数では、<a href="/ja/docs/Web/JavaScript/Reference/Operators/new">new</a> 演算子を介して呼び出されたと仮定して、関数自体を参照します。それ以外の場合、<code>new.target</code> は {{jsxref("undefined")}} です。</p>
<p><a href="/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions">アロー関数</a>では、<code>new.target</code> は周囲のスコープから継承されます。</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="new.target_in_function_calls" name="new.target_in_function_calls">関数呼び出しにおいての <code>new.target</code> の使用</h3>
<p>通常の関数呼び出しでは(コンストラクタ関数の呼び出しとは反対に)、<code>new.target</code> は {{jsxref("undefined")}} です。これにより、関数がコンストラクタとして <a href="/ja/docs/Web/JavaScript/Reference/Operators/new">new</a> で呼び出されたかを検出できます。</p>
<pre class="brush: js notranslate">function Foo() {
if (!new.target) { throw 'Foo() must be called with new' }
console.log('Foo instantiated with new')
}
new Foo() // logs "Foo instantiated with new"
Foo() // throws "Foo() must be called with new"
</pre>
<h3 id="new.target_in_constructors" name="new.target_in_constructors">コンストラクタにおいての new.target</h3>
<p>クラスコンストラクタで、<code>new.target</code> は <code>new</code> で直接実行されたコンストラクタを参照します。これは、コンストラクタは親クラスにあり、子コンストラクタからデリゲートされた場合も同様です。</p>
<pre class="brush: js notranslate">class A {
constructor() {
console.log(new.target.name)
}
}
class B extends A { constructor() { super() } }
let a = new A() // logs "A"
let b = new B() // logs "B"
class C { constructor() { console.log(new.target) } }
class D extends C { constructor() { super() } }
let c = new C() // logs class C{constructor(){console.log(new.target);}}
let d = new D() // logs class D extends C{constructor(){super();}}</pre>
<p class="summary">上記のクラス <code>C</code> と <code>D</code> の例から、<code>new.target</code> は初期化されたクラスのクラス定義を指しているように見えます。たとえば、<code>d</code> を <code>new D()</code> で初期化した場合は、<code>D</code> のクラス定義が出力され、同様に <code>c</code> の場合は <code>C</code> のクラスが出力されます。</p>
<h2 id="Specifications" name="Specifications">仕様</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">仕様書</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td>
</tr>
</tbody>
</table>
<h2 id="Specifications" name="Specifications">仕様</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">仕様書</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2>
<p>{{Compat("javascript.operators.new_target")}}</p>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li><a href="/ja/docs/Web/JavaScript/Reference/Functions">関数</a></li>
<li><a href="/ja/docs/Web/JavaScript/Reference/Classes">クラス</a></li>
<li><code><a href="/ja/docs/Web/JavaScript/Reference/Operators/new">new</a></code></li>
<li><code><a href="/ja/docs/Web/JavaScript/Reference/Operators/this">this</a></code></li>
</ul>
|