C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript WeakMap set() methodThe JavaScript WeakMap set() method is used to add or update an element to WeakMap object with the particular key-value pair. Each value must have a unique key (i.e. object). SyntaxThe set() method is represented by the following syntax: weakMapObj.set(key,value) Parameterkey - It represents the key to be added. value - It represents the value to be added. ReturnThe WeakMap object. JavaScript WeakMap set() method exampleHere, we will understand set() method through various examples. Example 1Let's see an example to add elements to WeakMap object. <script> var wm = new WeakWeakMap(); var obj1 = {}; var obj2 = {}; var obj3= {}; wm.set(obj1, "jQuery"); wm.set(obj2, "AngularJS"); wm.set(obj3,"Bootstrap"); document.writeln(wm.get(obj1)+"<br>"); document.writeln(wm.get(obj2)+"<br>"); document.writeln(wm.get(obj3)); </script> Output: jQuery AngularJS Bootstrap Example 2Let's see an example to add elements to WeakMap object in chainable form. <script> var wm = new WeakMap(); var obj1 = {}; var obj2 = {}; var obj3= {}; wm.set(obj1, "jQuery").set(obj2, "AngularJS").set(obj3,"Bootstrap"); document.writeln(wm.get(obj1)+"<br>"); document.writeln(wm.get(obj2)+"<br>"); document.writeln(wm.get(obj3)); </script> Output: jQuery AngularJS Bootstrap Example 3In this example, we will determine the result when different values are added to the same object. <script> var wm = new WeakMap(); var obj1 = {}; var obj2 = {}; wm.set(obj1, "jQuery"); wm.set(obj2, "AngularJS"); wm.set(obj2,"Bootstrap"); document.writeln(wm.get(obj1)+"<br>"); document.writeln(wm.get(obj2)+"<br>"); document.writeln(wm.get(obj2)); </script> Output: jQuery Bootstrap Bootstrap
Next TopicJavaScript WeakMap
|