概要
オブジェクトが封印されているかを判定します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
Object.isSealed(obj)
引数
- obj
- 確認したいオブジェクトです。
Return value
指定されたオブジェクトが封印されているかどうかを示すBoolean。
説明
オブジェクトが封印されている場合は true が、そうでない場合は false が返ります。オブジェクトが 拡張不可 かつすべてのプロパティが設定変更不可であり、それゆえ削除できない場合 (ただし書き込み不可である必要はありません) 場合に、封印されているとなります。
例
// 既定でオブジェクトは封印されていません
var empty = {};
Object.isSealed(empty); // === false
// 空のオブジェクトを拡張不可にすると、そのまま封印状態になります
Object.preventExtensions(empty);
Object.isSealed(empty); // === true
// 空でないオブジェクトでは、そのプロパティをすべて設定変更不可にしない限り上記と同じにはなりません
var hasProp = { fee: 'fie foe fum' };
Object.preventExtensions(hasProp);
Object.isSealed(hasProp); // === false
// そこですべてのプロパティを設定変更不可にすると、オブジェクトは封印状態になります
Object.defineProperty(hasProp, 'fee', {
configurable: false
});
Object.isSealed(hasProp); // === true
// オブジェクトを封印する最も簡単な方法は、もちろん Object.seal です
var sealed = {};
Object.seal(sealed);
Object.isSealed(sealed); // === true
// 封印されたオブジェクトはその定義により、拡張できません
Object.isExtensible(sealed); // === false
// 封印されたオブジェクトは凍結されているかもしれませんが、必ずしもそうではありません
Object.isFrozen(sealed); // === true
// すべてのプロパティが書き込み不能でもあります
var s2 = Object.seal({ p: 3 });
Object.isFrozen(s2); // === false
// "p" は依然書き込み可能です
var s3 = Object.seal({ get p() { return 0; } });
Object.isFrozen(s3); // === true
// アクセサプロパティでは設定変更が可能かという事柄だけになります
補足
In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError. In ES2015, a non-object argument will be treated as if it was a sealed ordinary object, simply return true.
Object.isSealed(1); // TypeError: 1 is not an object (ES5 code) Object.isSealed(1); // true (ES2015 code)
ブラウザの互換性
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.
Update compatibility data on GitHub
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
isSealed | Chrome 完全対応 6 | Edge 完全対応 12 | Firefox 完全対応 4 | IE 完全対応 9 | Opera 完全対応 12 | Safari 完全対応 5.1 | WebView Android 完全対応 1 | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 12 | Safari iOS 完全対応 6 | Samsung Internet Android 完全対応 1.0 | nodejs 完全対応 あり |
凡例
- 完全対応
- 完全対応