The handler.ownKeys function in JavaScript serves to yield an enumerable object. This method acts as a trap for Reflect.ownKeys.
Syntax
Example
ownKeys: function(target)
Parameters
target : The target object.
Return value
Return an enumerable object.
Browser Support
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 18 |
| Opera | 36 |
Example 1
Example
<script>
var proxy1 = new Proxy({}, {
ownKeys: function(target) {
document.writeln(" Use handler.ownKeys() is ");
return ['a', 'b', 'c'];
}
});
document.writeln(Object.getOwnPropertyNames(proxy1));
//expected output: Use handler.ownKeys() is a,b,c
</script>
Output:
Output
Use handler.ownKeys() is a,b,c
Example 2
Example
<script>
var x = {
Age:24,
Count: 45
}
var y = {
ownKeys (target) {
return Reflect.ownKeys(target)
}
}
var z = new Proxy(x,y);
for (let key of Object.keys(z))
{
document.writeln(key);
// expected output: Age Count
}
</script>
Output:
Output
Age Count
Example 3
Example
<script>
var x = { foo: 1 };
Object.defineProperty(x, 'variable', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
var proxy = new Proxy(x, {
ownKeys: function(target, a, b) {
document.writeln('in ownKeys method');
var names = Object.getOwnPropertyNames(target);
//Useing Object.getOwnProperty mehod.
var symbols = Object.getOwnPropertySymbols(target);
return names.concat(symbols);
}
});
document.writeln(Object.keys(proxy));
//expected.output: in ownKeys method foo
document.writeln("<br/>")
document.writeln(Object.getOwnPropertyNames(proxy));
//expected output: in ownKeys method foo,variable
</script>
Output:
Output
in ownKeys method foo
in ownKeys method foo,variable