From f6ceeec73546dae8d4b7847cc81742dc8bae3abb Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Thu, 17 Feb 2022 22:40:50 +0900 Subject: 2021/07/21 時点の英語版に同期 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reference/operators/new.target/index.md | 57 ++++++++++------------ 1 file changed, 26 insertions(+), 31 deletions(-) (limited to 'files/ja') diff --git a/files/ja/web/javascript/reference/operators/new.target/index.md b/files/ja/web/javascript/reference/operators/new.target/index.md index ba65c7b49f..f26949cf0d 100644 --- a/files/ja/web/javascript/reference/operators/new.target/index.md +++ b/files/ja/web/javascript/reference/operators/new.target/index.md @@ -2,22 +2,25 @@ title: new.target slug: Web/JavaScript/Reference/Operators/new.target tags: -- Classes -- ECMAScript 2015 -- JavaScript -- 言語機能 -- リファレンス + - クラス + - ECMAScript 2015 + - JavaScript + - 言語機能 + - リファレンス +browser-compat: javascript.operators.new_target translation_of: Web/JavaScript/Reference/Operators/new.target --- {{JSSidebar("Operators")}} -`new.target` は擬似プロパティで、関数やコンストラクターが [new](/ja/docs/Web/JavaScript/Reference/Operators/new) 演算子を使用して呼び出されたかどうかを検出することができます。 [new](/ja/docs/Web/JavaScript/Reference/Operators/new) 演算子を使用して呼び出されたコンストラクターや関数の中では、 `new.target` はコンストラクターや関数への参照を返します。通常の関数呼び出しの場合、 `new.target` は {{jsxref("undefined")}} になります。 +**`new.target`** は擬似プロパティで、関数やコンストラクターが [new](/ja/docs/Web/JavaScript/Reference/Operators/new) 演算子を使用して呼び出されたかどうかを検出することができます。 [new](/ja/docs/Web/JavaScript/Reference/Operators/new) 演算子を使用して呼び出されたコンストラクターや関数の中では、 `new.target` はコンストラクターや関数への参照を返します。通常の関数呼び出しの場合、 `new.target` は {{jsxref("undefined")}} になります。 {{EmbedInteractiveExample("pages/js/expressions-newtarget.html")}} ## 構文 -
new.target
+```js +new.target +``` ## 解説 @@ -33,24 +36,26 @@ translation_of: Web/JavaScript/Reference/Operators/new.target ## 例 -

関数呼び出しにおける new.target の使用

+### 関数呼び出しにおける new\.target の使用 通常の関数呼び出しでは (コンストラクター関数の呼び出しとは対照的に)、 `new.target` は {{jsxref("undefined")}} になります。これにより、関数が [new](/ja/docs/Web/JavaScript/Reference/Operators/new) でコンストラクターとして呼び出されたかを検出できます。 -
function Foo() {
+```js
+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"
-
+new Foo() // "Foo instantiated with new" を出力 +Foo() // "Foo() must be called with new" 例外が発生 +``` -

コンストラクターにおける new.target

+### コンストラクターにおける new\.target クラスのコンストラクターでは、`new.target` は `new` で直接実行されたコンストラクターを参照します。これは、コンストラクターが親クラスにあり、子コンストラクターから委任された場合も同様です。 -
class A {
+```js
+class A {
   constructor() {
     console.log(new.target.name)
   }
@@ -65,32 +70,22 @@ 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();}}
+let d = new D() // logs class D extends C{constructor(){super();}} +``` -

上記の `C` および `D` クラスの例から、 `new.target` は初期化されたクラスのクラス定義を指しているように見えます。たとえば、`d` を `new D()` で初期化した場合は、 `D` のクラス定義が出力され、同様に `c` の場合は `C` のクラスが出力されます。

+上記の `C` および `D` クラスの例から、 `new.target` は初期化されたクラスのクラス定義を指しているように見えます。たとえば、`d` を `new D()` で初期化した場合は、 `D` のクラス定義が出力され、同様に `c` の場合は `C` のクラスが出力されます。 ## 仕様書 - - - - - - - - - -
仕様書
{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}
+{{Specifications}} ## ブラウザーの互換性 -{{Compat("javascript.operators.new_target")}} +{{Compat}} ## 関連情報 - +- [`new`](/ja/docs/Web/JavaScript/Reference/Operators/new) +- [`this`](/ja/docs/Web/JavaScript/Reference/Operators/this) -- cgit v1.2.3-54-g00ecf