Reflect.preventExtensions()
静的な Reflect.preventExtensions() メソッドは、すでにプロパティが追加されたオブジェクトに、新しいプロパティを抑制します (つまり、オブジェクトのさらなる拡張を抑制します)。 Object.preventExtensions() に似ていますが、違いがあります。
試してみましょう
構文
Reflect.preventExtensions(target)
引数
target-
拡張を抑止する対象のオブジェクト。
返値
Boolean で、対象が拡張の抑止の設定に成功したかどうかを表します。
例外
解説
Reflect.preventExtensions メソッドは、すでにプロパティが追加されたオブジェクトに、新しいプロパティを抑制します (つまり、オブジェクトのさらなる拡張を抑制します)。これは Object.preventExtensions() と同様のメソッドです。
例
Reflect.preventExtensions() の使用
Object.preventExtensions() も参照してください。
js
// オブジェクトは既定で拡張可能。
let empty = {}
Reflect.isExtensible(empty) // === true
// ...しかし、変更できる。
Reflect.preventExtensions(empty)
Reflect.isExtensible(empty) // === false
Object.preventExtensions() との違い
このメソッドへの最初の引数がオブジェクトではない (プリミティブである) 場合、 TypeError が発生します。 Object.preventExtensions() では、 target がオブジェクトではない場合はオブジェクトに強制的に変換されます。
js
Reflect.preventExtensions(1)
// TypeError: 1 is not an object
Object.preventExtensions(1)
// 1
仕様書
| Specification |
|---|
| ECMAScript Language Specification # sec-reflect.preventextensions |
ブラウザーの互換性
BCD tables only load in the browser