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"
構文
Reflect.set(target, propertyKey, value[, receiver])
引数
target
-
プロパティを設定する対象のオブジェクト。
propertyKey
-
設定するプロパティ名。
value
-
設定する値。
receiver
省略可-
セッターによって
target
が呼び出されたときのthis
値を提供する。
返値
プロパティが成功裏に設定できたかどうかを示す Boolean
値。
例外
解説
Reflect.set
メソッドは、オブジェクトにプロパティを設定します。これはプロパティの割り当てを行い、機能としては プロパティアクセサー 構文のようなものです。
例
Reflect.set() の使用
js
// オブジェクト
let obj = {};
Reflect.set(obj, "prop", "value"); // true
obj.prop; // "value"
// 配列
let arr = ["duck", "duck", "duck"];
Reflect.set(arr, 2, "goose"); // true
arr[2]; // "goose"
// 配列を切り詰められる。
Reflect.set(arr, "length", 1); // true
arr; // ["duck"]
// 引数が1つだけだと、プロパティキーと値は "undefined" になる。
let 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 GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
set |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.