TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript handler preventExtensions() Method

JavaScript handler preventExtensions() Method with examples on javascript handler, javascript methods, javascript tutorial, apply(), construct(), defineProperty(), deleteProperty(), get(), setPrototypeOf(), set(), ownKeys(), isExtensible() etc.

<< Back to JAVASCRIPT

JavaScript handler.preventExtensions() Method

The handler.preventExtensions() method is used to trap the Object.preventExtensions method. When extensions are prevented on an object, new properties can't be added.

Syntax

preventExtensions: function(target)

Parameters

target: The target object.

Return value

Return a Boolean value.

Browser Support

Chrome 49
Edge 12
Firefox 22
Opera 36

Example 1

<script>
const proxy = new Proxy({}, {
  preventExtensions: function(target) {
   Object.preventExtensions(target);
    return !Object.isExtensible(target);
  }
});
document.writeln(Object.isExtensible(proxy));
//expected output:true
</script>
Test it Now

Output:

true

Example 2

<script>
const  proxy1 = new Proxy({}, {
  preventExtensions: function(target) {
  

    Object.preventExtensions(target);
    return !Object.isExtensible(target);
  }
});
document.writeln(Object.isExtensible(proxy1));
//expected output: true 
document.writeln('
'); document.writeln(Object.preventExtensions(proxy1)); // Object.preventExtensions prevent of object to access. //expected output: [object Object] document.writeln('
'); document.writeln(Object.isExtensible(proxy1)); //expected output: false </script>
Test it Now

Output:

true 
[object Object] 
false

Example 3

<script>
var x = {
  first: false
};
var y ={
  preventExtensions(target) {
    target.canEvolve = false;
    Object.preventExtensions(target);
    return true;
  }
};
var proxy = new Proxy(x, y);
document.writeln(x.first);
// expected output: false
Object.preventExtensions(proxy);
document.writeln("
"); document.writeln(x.first); // expected output: false </script>
Test it Now

Output:

false
 false

Next TopicJavaScript handler




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf