preventExtensions() - JavaScript Tutorial

preventExtensions()

BLUF: This tutorial on preventExtensions() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: preventExtensions()

Understanding preventExtensions() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The Reflect.preventExtensions method is employed to prohibit any further extensions to an object. It serves the same purpose as the Object.preventExtensions method.

Syntax:

Example

Reflect.preventExtensions(obj)

Parameters:

Subject: This is the target entity for which extensions are to be restricted.

Return value:

The function will return a boolean value of true if the target has been effectively set to block extensions. If not successful, the function will return false.

Exceptions:

A TypeError, if the target is not an Object.

Browser Support:

Chrome 49
Edge 12
Firefox 42
Opera 36

Example 1

Example

const u = {};
console.log ( Reflect.isExtensible ( u ) ); 
Reflect.preventExtensions ( u );
console.log ( Reflect.isExtensible ( u ) );

Output:

Output

true
false

Example 2

Example

const obj = {"p":3,"q":4};
Reflect.preventExtensions(obj);
console.log ( obj.hasOwnProperty ( "p" ) ); 
delete obj.p;
console.log ( obj.hasOwnProperty ( "q" ) );

Output:

Output

true
true

Example 3

Example

const uu = {};
Reflect.preventExtensions(uu);
console.log(
Reflect.isExtensible(uu)
); 
// add a property
uu.pp = 3;
console.log(
    uu.hasOwnProperty ("pp")
);

Output:

Output

false
false

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience