Reflect.set()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

Reflect.set() 정적 메서드는 객체 속성의 값을 설정합니다.

시도해보기

const object1 = {};
Reflect.set(object1, "property1", 42);

console.log(object1.property1);
// Expected output: 42

const array1 = ["duck", "duck", "duck"];
Reflect.set(array1, 2, "goose");

console.log(array1[2]);
// Expected output: "goose"

구문

js
Reflect.set(target, propertyKey, value[, receiver])

매개변수

target

속성의 값을 설정할 대상 객체.

propertyKey

값을 설정할 속성의 이름.

value

설정할 값.

receiver Optional

속성이 설정자일 경우, this로 사용할 값.

반환 값

값 설정의 성공 여부를 나타내는 Boolean.

예외

targetObject가 아니면 TypeError.

설명

Reflect.set() 메서드는 객체 속성의 값을 설정할 수 있습니다. 속성 추가도 할 수 있으며, 함수라는 점을 제외하면 동작 방식은 속성 접근자와 같습니다.

예제

Reflect.set() 사용하기

js
// Object
var obj = {};
Reflect.set(obj, "prop", "value"); // true
obj.prop; // "value"

// Array
var arr = ["duck", "duck", "duck"];
Reflect.set(arr, 2, "goose"); // true
arr[2]; // "goose"

// 배열 자르기
Reflect.set(arr, "length", 1); // true
arr; // ["duck"];

// 매개변수를 하나만 제공하면 속성 키 이름은 문자열 "undefined", 값은 undefined
var obj = {};
Reflect.set(obj); // true
Reflect.getOwnPropertyDescriptor(obj, "undefined");
// { value: undefined, writable: true, enumerable: true, configurable: true }

명세

Specification
ECMAScript® 2025 Language Specification
# sec-reflect.set

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
set

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

같이 보기