LoginSignup
1
1

More than 1 year has passed since last update.

プロパティ記述子って何ですか?

Posted at

JavaScript オブジェクトのプロパティは、「書き込み禁止かどうか」とか「各種設定可能かどうか」といった、いくつかの属性を持っています。
これらをプロパティ属性といいます。

プロパティ属性は、プロパティ記述子というオブジェクトのプロパティとして保持されています。
プロパティ記述子は、データ記述子アクセサー記述子があります。

データ記述子

属性 説明
value プロパティに設定された値
configurable プロパティ記述子の設定変更可否
enumerable for...inループの列挙可能性
writable 値の変更可否

アクセサー記述子

属性 説明
get ゲッターと呼ばれる関数を格納する場所
set セッターと呼ばれる関数を格納する場所

データ記述子の確認

const obj = {prop: ""};
const propDesc = Reflect.getOwnPropertyDescriptor(obj, "prop");
console.log(propDesc); // => { value: '値', writable: true, enumerable: true, configurable: true }

const obj2 = ['りんご', 'バナナ'];
const propDesc2 = Reflect.getOwnPropertyDescriptor(obj2, 'length');
console.log(propDesc2); // => { value: 2, writable: true, enumerable: false, configurable: false }
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1