for of loop [SOLVED]



  • Sry if its a silly question, I'm not soo good in JavaScript 😕 Why can't I use:

    for (let room of Game.rooms) {
      room.lookAt(1,1);
    }

    If i use "room IN Game.rooms" then I can't access the lookAt function again because I get the error:
    "TypeError: room.lookAt is not a function"

     

    Solution:
    for (let room in Game.rooms) {
      Game.rooms[room].lookAt(1,1);
    }

    Thx Atavus for the answer! 🙂



  • for of only works with collections which can iterate (eg arrays).

    for in iterates over the keys of an object. So if you do for let roomName in Game.rooms and want to do something with the room, you do Game.rooms[roomName].lookAt.