This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Reflect.construct()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Itโ€™s been available across browsers since โจ2016๋…„ 9์›”โฉ.

Reflect.construct() ์ •์  ๋ฉ”์„œ๋“œ๋Š” new ์—ฐ์‚ฐ์ž์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. new target(...args)๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์œผ๋กœ ๋‹ค๋ฅธ ํ”„๋กœํ† ํƒ€์ž…์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ๋„ํ•ด ๋ณด๊ธฐ

function func1(a, b, c) {
  this.sum = a + b + c;
}

const args = [1, 2, 3];
const object1 = new func1(...args);
const object2 = Reflect.construct(func1, args);

console.log(object2.sum);
// Expected output: 6

console.log(object1.sum);
// Expected output: 6

๊ตฌ๋ฌธ

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

๋งค๊ฐœ๋ณ€์ˆ˜

target

ํ˜ธ์ถœํ•  ๋Œ€์ƒ ํ•จ์ˆ˜.

argumentsList

target์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•  ๋ฐฐ์—ดํ˜• ๊ฐ์ฒด.

newTarget Optional

ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉํ•  ์ƒ์„ฑ์ž. new.target ์—ฐ์‚ฐ์ž๋„ ํ™•์ธํ•˜์„ธ์š”. newTarget์ด ์ฃผ์–ด์ง€์ง€ ์•Š์•˜์„ ๋• target์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ ๊ฐ’

target์„ ์ƒ์„ฑ์ž๋กœ ํ•˜๊ณ  ์ฃผ์–ด์ง„ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด ์ƒ์„ฑํ•œ target(๋˜๋Š”, ์ง€์ •ํ–ˆ๋‹ค๋ฉด newTarget)์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค.

์˜ˆ์™ธ

target ๋˜๋Š” newTarget์ด ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹ˆ๋ฉด TypeError.

์„ค๋ช…

Reflect.construct()๋Š” ์ƒ์„ฑ์ž๋ฅผ ๊ฐ€๋ณ€ ๊ธธ์ด์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (new ์—ฐ์‚ฐ์ž์™€ ์ „๊ฐœ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค)

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

Reflect.construct() vs Object.create()

Reflect์˜ ๋„์ž… ์ด์ „์—๋Š” ์ž„์˜์˜ ์ƒ์„ฑ์ž์™€ ํ”„๋กœํ† ํƒ€์ž…์— Object.create()๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

js
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()์™€ Function.prototype.apply()๋ฅผ ์‚ฌ์šฉํ•  ๋•, ๊ฐ์ฒด ์ƒ์„ฑ์— new ํ‚ค์›Œ๋“œ๊ฐ€ ๊ด€์—ฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ new.target ์—ฐ์‚ฐ์ž๊ฐ€ undefined๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด Reflect.construct()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด, newTarget์ด ์กด์žฌํ•˜๋ฉด new.target ์—ฐ์‚ฐ์ž๊ฐ€ newTarget์„, ์•„๋‹ˆ๋ฉด target์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

js
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() ์‚ฌ์šฉํ•˜๊ธฐ

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

๋ช…์„ธ

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-reflect.construct

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ