The handler.get function serves as a mechanism for retrieving the value of a property. This function accepts three parameters.
Syntax
Example
get: function(target, property, receiver)
Parameters
Target : The target object.
Property : Name of the property to get.
Receiver: The proxy itself or any object that derives from the proxy.
Return value
This method can return any value.
Browser Support
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 18 |
| Opera | 36 |
Example 1
Example
var data = {
"a": 11,"b": 21 }
var get = new Proxy(
data , {
get: function(y, idx) {
return y[idx] * 2
document.writeln("<br/>");
}
}
)
for(var z in get) {
document.write(z +":")
document.write(get[z])
}
Output:
Output
a:22b:42
Example 2
Example
var x = { f: 45, g:23 };
var proxy = new Proxy(x, {
get: function(target, name, proxy) {
document.write('In get');
var value = target[name];
if (typeof value === 'string') {
value = value.toUpperCase();
}
return value;
}
});
document.write (proxy.f);
document.write (x.g);
Output:
Output
In get4523
Example 3
Example
const r = {}
const p = new Proxy(r, {
get: function(target, property, receiver) {
document.write('called: ' + property);
return 100;
}
});
document.write(p. a);
Output:
Output
called: a100