diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/web/javascript/reference/operators/new.target/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/web/javascript/reference/operators/new.target/index.html')
-rw-r--r-- | files/ja/web/javascript/reference/operators/new.target/index.html | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/files/ja/web/javascript/reference/operators/new.target/index.html b/files/ja/web/javascript/reference/operators/new.target/index.html new file mode 100644 index 0000000000..b00f08cb4e --- /dev/null +++ b/files/ja/web/javascript/reference/operators/new.target/index.html @@ -0,0 +1,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> |