0
<!DOCTYPE html>
<html>
<body>

<p>A function is triggered when the user is pressing a key in the input field.</p>

<input type="text" onkeydown="myFunction(event)">

<script>
function myFunction(event)
{
  console.log(Object.keys(event))
  console.log(event)
}
</script>

</body>
</html>

I run above code in: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown I expect get all KeyboardEvent object properties that documented in MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

My goal is to get the property of the difference between two objects in javascript to compare two KeyboardEvent objects, but the linked solution does not work for the KeyboardEvent object, Object.keys(KeyboardEvent) only get one "isTrusted" key rather than all keys.

nicoring
  • 633
  • 5
  • 12
illiterate
  • 289
  • 2
  • 12
  • 2
    Not all properties are enumerable, and not all properties are on the object itself -- they can be inherited. See referenced Q&A that provides a solution. – trincot Dec 08 '19 at 13:40

1 Answers1

1

Object.keys only iterate over whose enumerable property is true mozilla doc

function myFunction(event){

  console.log(Object.keys(event))
  console.log(event)
  
  for (const property in event) {
 
 if(typeof event[property] == "function"){ 
  console.log(`${property}: `+':its a  fuction');
  }else{
   console.log(`${property}: ${event[property]}`+':  its a property');
  }
}
}
<!DOCTYPE html>
<html>
<body>

<p>A function is triggered when the user is pressing a key in the input field.</p>

<input type="text" onkeydown="myFunction(event)">

</body>
</html>
Jadli
  • 858
  • 1
  • 9
  • 17