Session 8

Advanced Javascript

objects and classes

23. Nov. 2022, 17:00-18:30

Artful Coding 1: web-based games development

dieAngewandte

objects

is there anything that isn't one?

The most important thing you need to know about objects:
JavaScript Objects @ w3schools


            // practical if you want to bundle properties and methods
            const player = {
              name: "Balthazaaar",
              type: "dirt-devil",
              health: 100,
              xp: 0,
              quests: [],
              acceptQuest: function (questID) {
                this.quests.push(questID)
                // do something
              }
            }
          

classes

a factory for making objects

All the fuzz about class:
JavaScript Class Syntax @ w3schools


            // practical e.g. if we do not just only want ONE player, but many characters
            class Character {
              constructor (name, type) {
                this.name = name
                this.type = type
                this.health = 100
                this.xp = 0
                this.quests = []

                if (type == 'wonderwuzz') {
                  this.xp = 1000
                }
              }

              acceptQuest (questID) {
                this.quests.push(questID)
                // do something interesting
              }
            }

            const playerB = new Character('Balthazaaar', 'dirt-devil')
            const playerA = new Character('Ada', 'wonderwuzz')

            playerA.acceptQuest(23)
            playerB.acceptQuest(42)
            playerB.acceptQuest(5)

            console.log(playerA)
            console.log(playerB)