handler.preventExtensions()
handler.preventExtensions()
は Object.preventExtensions()
に対するトラップです。
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.
構文
const p = new Proxy(target, {
preventExtensions: function(target) {
}
});
引数
次の引数は preventExtensions()
メソッドに渡されます。 this
はハンドラーにバインドされます。
target
- ターゲットオブジェクトです。
返値
preventExtensions()
メソッドは真偽値を返さなければなりません。
解説
handler.preventExtensions()
メソッドは Object.preventExtensions()
に対するトラップです。
介入
このトラップは下記の操作に介入できます。
不変条件
以下の不変条件に違反している場合、プロキシは TypeError
を発生します。
Object.preventExtensions(proxy)
は、Object.isExtensible(proxy)
がfalse
の場合のみtrue
を返します。
例
preventExtensions のトラップ
次のコードでは Object.preventExtensions()
をトラップします。
const p = new Proxy({}, {
preventExtensions: function(target) {
console.log('called');
Object.preventExtensions(target);
return true;
}
});
console.log(Object.preventExtensions(p)); // "called"
// false
The following code violates the invariant.
const p = new Proxy({}, {
preventExtensions: function(target) {
return true;
}
});
Object.preventExtensions(p); // TypeError is thrown
仕様書
ブラウザーの互換性
BCD tables only load in the browser