Skip to content

Instantly share code, notes, and snippets.

@case-eee
Created March 28, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save case-eee/7e87e60fd6b8cadd0a46fc8872017614 to your computer and use it in GitHub Desktop.
Save case-eee/7e87e60fd6b8cadd0a46fc8872017614 to your computer and use it in GitHub Desktop.
Monsters! ES5 to ES6
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