C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript handler.ownKeys() MethodThe handler.ownKeys() method of JavaScript is used to return an enumerable object. This method is a trap for Reflect.ownKeys(). Syntax
ownKeys: function(target) Parameters
target: The target object. Return valueReturn an enumerable object. Browser Support
Example 1
<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: Use handler.ownKeys() is a,b,c Example 2
<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: Age Count Example 3
<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("Output: in ownKeys method foo in ownKeys method foo,variable
Next TopicJavaScript handler
|