TheDeveloperBlog.com

Home | Contact Us

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

JavaScript handler set() Method

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

<< Back to JAVASCRIPT

JavaScripthand handler.set() Method

The handler.set method is an assignment of a value to a property value. The set can be iterator in the insertion order. It is a collection of items which are unique. If an assignment succeeded, then return true. Otherwise, returns false.

An Iterator is an object which defines a sequence and potentially a return value upon its termination.

Syntax

set: function(target, property, value, receiver)

Parameters

target: The target object.

property: Name or Symbol of the property.

value: New value of the property.

receiver: This is usually the proxy itself. A set handler assignment indirectly, via the prototype chain or various other ways.

Return value

Return a Boolean value.

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

<script>
var proxy = { "a": 1, "b": 2 }
var pset = new Proxy(
  proxy, {
    set: function(y, idx, value) {
    y[idx] = value * 10
   }
  }
)
pset.c = 3
for(var z in pset) {
  document.writeln(z + "=" )
  // print equal sign.
  document.writeln(pset[z])
  
  //expected output: a= 1 b= 2 c= 30
}
</script>
Test it Now

Output:

a= 1 b= 2 c= 30

Example 2

<script>
var x = { foo: 1 };
var proxy = new Proxy(x, {
  set: function(target, name, value, proxy) {
  document.writeln ('in set');
target[name] = value.toUpperCase();
  }
});
proxy.foo = 'proxy method';
document.writeln(x.foo); // value stored on x is uppercase
 //expected output: in set PROXY METHOD
 </script>
Test it Now

Output:

in set PROXY METHOD

Example 3

<script>
const q={
 foo:1
}
const p = new Proxy(q, {
  set: function(target, prop, value, receiver) {
    target[prop] = value;
    document.writeln('property set: ' + prop + ' = ' + value);
    //expected output: property set: a = 10
    }
})
document.writeln('a' in p); 
//expected output: false
p.a = 10;  
// "property set: a = 10"
</script>
Test it Now

Output:

false property set: a = 10

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