Object.seal()【封印】メソッド
メモ
- 封印 (プロパティの追加・削除 不可)
- 元のオブジェクトを変更
- 封印の解除は不可
- プロパティ操作
- 通常モード:プロパティの追加・削除は無視
- strict モード:プロパティの追加・削除は TypeError 例外
- 参照:プロパティの変更・追加・削除 防止
構文
- Object.seal ( O )
- O:オブジェクト
- 封印されたオブジェクト
- 入力のO (オブジェクト以外)
- TypeError 例外:O がオブジェクト以外
- TypeError 例外:封印されたプロパティの追加・削除 処理 (strict モード)
例
// プロパティの変更・追加・削除
var pointA = { x:0, y:0 };
console.log(pointA); // 出力:Object {x: 0, y: 0}
pointA.x = 100;
delete pointA.y;
pointA.z = 300;
console.log(pointA); // 出力:Object {x: 100, z: 300}
// 封印されたプロパティの変更・追加・削除
var pointB = { x:0, y:0 };
console.log(pointB); // 出力:Object {x: 0, y: 0}
Object.seal(pointB);
pointB.x = 100;
delete pointB.y; // 失敗
pointB.z = 300; // 失敗
console.log(pointB); // 出力:Object {x: 100, y: 0}