Object.isExtensible()

Baseline Widely available

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

Object.isExtensible() メソッドは、オブジェクトが拡張可能であるか(新しいプロパティを追加することができるかどうか)を判定します。

試してみましょう

const object1 = {};

console.log(Object.isExtensible(object1));
// Expected output: true

Object.preventExtensions(object1);

console.log(Object.isExtensible(object1));
// Expected output: false

構文

js
Object.isExtensible(obj)

引数

obj

チェックするオブジェクトです。

返値

論理値で、与えられたオブジェクトが拡張可能であるかどうかを示します。

解説

オブジェクトは既定では拡張可能です。つまり、新しいプロパティの追加が可能であり、 [[Prototype]] プロパティに再代入することができます。オブジェクトは Object.preventExtensions(), Object.seal(), Object.freeze(), Reflect.preventExtensions() のいずれかを用いる事で拡張不能に設定する事が可能です。

Object.isExtensible の使用

js
// 新規のオブジェクトは拡張可能
const empty = {};
Object.isExtensible(empty); // true

// その設定は変える事が可能
Object.preventExtensions(empty);
Object.isExtensible(empty); // false

// seal メソッドで封印されたオブジェクトは拡張不可と定義される
const sealed = Object.seal({});
Object.isExtensible(sealed); // false

// freeze メソッドで凍結されたオブジェクトも拡張不可と定義される
const frozen = Object.freeze({});
Object.isExtensible(frozen); // false

オブジェクト以外の型強制

ES5 では、このメソッドの引数がオブジェクトではない場合(プリミティブの場合)、 TypeError が発生します。 ES2015 以降では、オブジェクトでない引数は、それが拡張不可能な通常のオブジェクトであるかのように扱われ、単に false を返します。

js
Object.isExtensible(1);
// TypeError: 1 is not an object (ES5 code)

Object.isExtensible(1);
// false                         (ES2015 code)

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-object.isextensible

ブラウザーの互換性

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
isExtensible

Legend

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

Full support
Full support

関連情報