C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScripthand handler.set() MethodThe 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
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>
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>
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>
Output: false property set: a = 10
Next TopicJavaScript handler
|