From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../global_objects/reflect/construct/index.html | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 files/ko/web/javascript/reference/global_objects/reflect/construct/index.html (limited to 'files/ko/web/javascript/reference/global_objects/reflect/construct/index.html') diff --git a/files/ko/web/javascript/reference/global_objects/reflect/construct/index.html b/files/ko/web/javascript/reference/global_objects/reflect/construct/index.html new file mode 100644 index 0000000000..e98f3204de --- /dev/null +++ b/files/ko/web/javascript/reference/global_objects/reflect/construct/index.html @@ -0,0 +1,154 @@ +--- +title: Reflect.construct() +slug: Web/JavaScript/Reference/Global_Objects/Reflect/construct +tags: + - ECMAScript 2015 + - JavaScript + - Method + - Reference + - Reflect +translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/construct +--- +
{{JSRef}}
+ +

Reflect.construct() 정적 메서드는 new 연산자처럼 동작하는 함수입니다. new target(...args)를 호출하는 것과 같습니다. 추가 기능으로 다른 프로토타입을 지정할 수도 있습니다.

+ +
{{EmbedInteractiveExample("pages/js/reflect-construct.html")}}
+ + + +

구문

+ +
Reflect.construct(target, argumentsList[, newTarget])
+
+ +

매개변수

+ +
+
target
+
호출할 대상 함수.
+
argumentsList
+
target의 매개변수로 전달할 배열형 객체.
+
newTarget {{optional_inline}}
+
프로토타입으로 사용할 생성자. new.target 연산자도 확인하세요. newTarget이 주어지지 않았을 땐 target을 사용합니다.
+
+ +

반환 값

+ +

target을 생성자로 하고 주어진 매개변수를 전달해 생성한 target(또는, 지정했다면 newTarget)의 새로운 인스턴스.

+ +

예외

+ +

target 또는 newTarget이 생성자가 아니면 {{jsxref("TypeError")}}.

+ +

설명

+ +

Reflect.construct()는 생성자를 가변 길이의 매개변수로 호출할 수 있습니다. (new 연산자전개 구문을 사용해도 가능합니다)

+ +
var obj = new Foo(...args);
+var obj = Reflect.construct(Foo, args);
+
+ +

Reflect.construct() vs Object.create()

+ +

Reflect의 도입 이전에는 임의의 생성자와 프로토타입에 {{jsxref("Object.create()")}}를 사용해 객체를 생성할 수 있었습니다.

+ +
function OneClass() {
+    this.name = 'one';
+}
+
+function OtherClass() {
+    this.name = 'other';
+}
+
+// Calling this:
+var obj1 = Reflect.construct(OneClass, args, OtherClass);
+
+// ...has the same result as this:
+var obj2 = Object.create(OtherClass.prototype);
+OneClass.apply(obj2, args);
+
+console.log(obj1.name); // 'one'
+console.log(obj2.name); // 'one'
+
+console.log(obj1 instanceof OneClass); // false
+console.log(obj2 instanceof OneClass); // false
+
+console.log(obj1 instanceof OtherClass); // true
+console.log(obj2 instanceof OtherClass); // true
+
+ +

그러나, 결과는 동일하더라도 과정에는 중요한 차이가 하나 존재합니다. Object.create()와 {{jsxref("Function.prototype.apply()")}}를 사용할 땐, 객체 생성에 new 키워드가 관여하지 않으므로 new.target 연산자가 undefined를 가리킵니다.

+ +

반면 Reflect.construct()를 호출하면, newTarget이 존재하면 new.target 연산자가 newTarget을, 아니면 target을 가리킵니다.

+ +
function OneClass() {
+    console.log('OneClass');
+    console.log(new.target);
+}
+function OtherClass() {
+    console.log('OtherClass');
+    console.log(new.target);
+}
+
+var obj1 = Reflect.construct(OneClass, args);
+// Output:
+//     OneClass
+//     function OneClass { ... }
+
+var obj2 = Reflect.construct(OneClass, args, OtherClass);
+// Output:
+//     OneClass
+//     function OtherClass { ... }
+
+var obj3 = Object.create(OtherClass.prototype);
+OneClass.apply(obj3, args);
+// Output:
+//     OneClass
+//     undefined
+
+ +

예제

+ +

Reflect.construct() 사용하기

+ +
var d = Reflect.construct(Date, [1776, 6, 4]);
+d instanceof Date; // true
+d.getFullYear(); // 1776
+
+ +

명세

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-reflect.construct', 'Reflect.construct')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-reflect.construct', 'Reflect.construct')}}{{Spec2('ESDraft')}} 
+ +

브라우저 호환성

+ + + +

{{Compat("javascript.builtins.Reflect.construct")}}

+ +

같이 보기

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