I have following code ( HERE is editable example - usage: type in input field and watch console):
function test(event)
{
let keys = Object.keys(event);
let keysOwnProp = Object.getOwnPropertyNames(event);
let keysForIn=[]; for(let k in event) { keysForIn.push(k); }
console.log('keysForIn',keysForIn);
console.log('keys',JSON.stringify(keys));
console.log('keysOwnProp',JSON.stringify(keysOwnProp));
}
<input oninput="test(event)" placeholder="type something">
Questions:
- Why only in
keysForIn
I see all(?) event fields/properties, but inkeys
andkeysOwnProp
only one:isTrusted
? - Is there an alternative for
keysForIn
(if yes, provide it) ?
It seems
Object.keys
&Object.getOwnPropertyNames
gives the properties which belongs solely to the object where asfor..in
loop will also give the inherited propertiesHere I copy-paste Grégory NEUT answer - which looks like is the best one:
Object.keys(...)
returns the names of the non-symbol enumerable properties of the object, but only the ones which are not inherited.For...in
iterates the names of the non-symbol enumerable properties of the object, inherited or not.Object.getOwnPropertyNames(...)
returns all non-symbol properties (enumerable or not).To my knowledge there is no alternative to
for ... in
to get inherited properties