From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../reference/operators/new.target/index.html | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 files/ko/web/javascript/reference/operators/new.target/index.html (limited to 'files/ko/web/javascript/reference/operators/new.target/index.html') diff --git a/files/ko/web/javascript/reference/operators/new.target/index.html b/files/ko/web/javascript/reference/operators/new.target/index.html new file mode 100644 index 0000000000..9c480c1513 --- /dev/null +++ b/files/ko/web/javascript/reference/operators/new.target/index.html @@ -0,0 +1,93 @@ +--- +title: new.target +slug: Web/JavaScript/Reference/Operators/new.target +tags: + - Classes + - ECMAScript 2015 + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Operators/new.target +--- +
{{JSSidebar("Operators")}}
+ +

new.target 속성(property)은 함수 또는 생성자가 new 연산자를 사용하여 호출됐는지를 감지할 수 있습니다. new 연산자로 인스턴스화된 생성자 및 함수에서, new.target은 생성자 또는 함수 참조를 반환합니다. 일반 함수 호출에서는, new.target은 {{jsxref("undefined")}}입니다.

+ +
{{EmbedInteractiveExample("pages/js/expressions-newtarget.html")}}
+ + + +

구문

+ +
new.target
+ +

설명

+ +

new.target 구문은 키워드 "new", 점 및 속성명 "target"으로 구성됩니다. 보통 "new."은 속성 접근을 위한 문맥(context)으로 제공하지만 여기서 "new."은 정말 객체가 아닙니다. 그러나, 생성자 호출에서 new.targetnew에 의해 호출된 생성자를 가리키고 그래서 "new."은 가상 문맥이 됩니다.

+ +

new.target 속성은 모든 함수가 이용할 수 있는 메타 속성입니다. 화살표 함수에서, new.target은 둘러싸는 함수의 new.target을 말합니다.

+ +

+ +

함수 호출에서 new.target

+ +

일반 함수 호출(생성자 함수 호출과는 반대로)에서, new.target은 {{jsxref("undefined")}}입니다. 이는 함수가 생성자로서 new로 호출된 경우를 감지할 수 있습니다.

+ +
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"
+
+ +

생성자에서 new.target

+ +

클래스 생성자에서, new.targetnew에 의해 직접 호출된 생성자를 가리킵니다. 이는 그 생성자가 부모 클래스에 있고 자식 생성자로부터 위임받은 경우도 그 경우입니다.

+ +
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"
+
+ +

명세

+ + + + + + + + + + + + + + + + + + + +
명세상태설명
{{SpecName('ES6', '#sec-built-in-function-objects', 'Built-in Function Objects')}}{{Spec2('ES6')}}초기 정의.
{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}{{Spec2('ESDraft')}} 
+ +

브라우저 호환성

+ +

{{Compat("javascript.operators.new_target")}}

+ +

같이 보기

+ + -- cgit v1.2.3-54-g00ecf