Object.preventExtensions()【拡張(プロパティ追加)不可 設定】メソッド
メモ
- 拡張不可に設定 (プロパティ追加不可)
- 元のオブジェクトを変更
- 拡張不可の解除は不可
- プロパティ操作
- 通常モード:プロパティの追加は無視
- strict モード:プロパティの追加は TypeError 例外
- 参照:プロパティの変更・追加・削除 防止
構文
- Object.preventExtensions ( 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.preventExtensions(pointB);
pointB.x = 100;
delete pointB.y;
pointB.z = 300; // 失敗
console.log(pointB); // 出力:Object {x: 100}