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
}
}
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)