0

I have found some sourcecode on a javascript project but couldn't understand a part of code that looks like this:

   keystate= {};
    document.addEventListener("keydown", function(event) {
    **keystate[event.keyCode] = true;**
});

document.addEventListener("keyup", function(event) {
delete keystate[event.keyCode]
});

The first problem I do not understand is the brackets that come after declaring of the object and then especially what the "= true" means? And a more ambigous question would be: is this a part of OOP (object oriented programming)?

Axeowny
  • 21
  • 7

1 Answers1

2

The square brackets just allows you to access a property by having its name in a string (rather than in an identifier as you would use in dot notation).

The = is an assignment operator.

true is a boolean literal.

These are all equivalent.

foo.bar = "something";
foo["bar"] = "something";
var property = "bar"; foo[property] = "something";
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Does this add the keycode of the current event as a property in the keystate object? If so what does this do? When a key is pressed ad it's keycode as a property in the keystate object and set it to true? – Axeowny Jul 31 '15 at 15:38