-
-
Save case-eee/7e87e60fd6b8cadd0a46fc8872017614 to your computer and use it in GitHub Desktop.
Monsters! ES5 to ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Store(location, monsters){ | |
this.title = "Monsters" | |
this.monsters = monsters || [] | |
this.location = location | |
} | |
Store.prototype.add = function (name) { | |
this.monsters.push(new Monster(name)); | |
} | |
Store.prototype.remove = function (id) { | |
id = parseInt(id); | |
this.monsters = this.monsters.filter(function (m) { | |
return m.id !== id; | |
}); | |
} | |
Store.prototype.find = function (id) { | |
id = parseInt(id); | |
return this.monsters.find(function (m) { | |
return m.id === id; | |
}); | |
} | |
function Monster(name, level) { | |
this.name = name; | |
this.id = Date.now(); | |
this.level = level || 1; | |
} | |
Monster.prototype.levelUp = function () { | |
this.level++; | |
}; | |
Monster.prototype.levelDown = function () { | |
if (this.level > 1) { | |
this.level--; | |
} | |
}; | |
Monster.prototype.toHTML = function () { | |
return ("<article class='monster-list-item' id=" + this.id + ">" + | |
"<h2>" + this.name + "</h2>" + | |
"<p class='monster-level'>Level <span class='monster-level-number'>" + this.level + "</span></p>" + | |
"<div class='monster-controls'>" + | |
"<button class='monster-levelup'>Level Up</button>" + | |
"<button class='monster-leveldown'>Level Down</button>" + | |
"<button class='monster-delete'>Delete</button>" + | |
"</div></article>"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment