From 0f6af6f4b20236696d8e3ea22b8f856bbb838bd8 Mon Sep 17 00:00:00 2001 From: faulkenfett11 <36997363+faulkenfett11@users.noreply.github.com> Date: Thu, 22 Mar 2018 14:42:00 -0700 Subject: [PATCH 1/2] JS II constructors complete --- src/class.js | 11 +++- src/prototype.js | 133 +++++++++++++++++++++++++++++------------------ 2 files changed, 91 insertions(+), 53 deletions(-) diff --git a/src/class.js b/src/class.js index 1ec26ec..4a94349 100644 --- a/src/class.js +++ b/src/class.js @@ -7,7 +7,16 @@ // for a potential password that will be compared to the `password` property. // Return true if the potential password matches the `password` property. Otherwise return false. -// code here + +class User { + constructor() { + this. + this. + } +} + + + // Part 2 // Create a class called `Animal` and a class called `Cat` using ES6 classes. diff --git a/src/prototype.js b/src/prototype.js index e2494a6..03954d5 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -1,55 +1,84 @@ -/* - Object oriented design is commonly used in video games. For this part of the assignment - you will be implementing several classes with their correct inheritance heirarchy. - - In this file you will be creating three classes: - GameObject - createdAt - dimensions - destroy() // prototype method -> returns the string 'Game object was removed from the game.' - - NPC - hp - name - takeDamage() // prototype method -> returns the string ' took damage.' - // should inherit destroy() from GameObject's prototype - - Humanoid - faction - weapons - language - greet() // prototype method -> returns the string ' offers a greeting in .' - // should inherit destroy() from GameObject through NPC - // should inherit takeDamage() from NPC - - Inheritance chain: Humanoid -> NPC -> GameObject - Instances of Humanoid should have all of the same properties as NPC and GameObject. - Instances of NPC should have all of the same properties as GameObject. - - Example: - - const hamsterHuey = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - hp: 5, - name: 'Hamster Huey', - faction: 'Gooey Kablooie', - weapons: [ - 'bubblegum', - ], - language: 'Hamsterish', - }); - - hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' - hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' - hamsterHuey.destroy(); // returns 'Game object was removed from the game.' -*/ - -/* eslint-disable no-undef */ +// /* +// Object oriented design is commonly used in video games. For this part of the assignment +// you will be implementing several classes with their correct inheritance heirarchy. +// In this file you will be creating three classes: +// GameObject +// createdAt +// dimensions +// destroy() // prototype method -> returns the string 'Game object was removed from the game.' +// NPC +// hp +// name +// takeDamage() // prototype method -> returns the string ' took damage.' +// // should inherit destroy() from GameObject's prototype +// Humanoid +// faction +// weapons +// language +// greet() // prototype method -> returns the string ' offers a greeting in .' +// // should inherit destroy() from GameObject through NPC +// // should inherit takeDamage() from NPC +// Inheritance chain: Humanoid -> NPC -> GameObject +// Instances of Humanoid should have all of the same properties as NPC and GameObject. +// Instances of NPC should have all of the same properties as GameObject. + +function GameObject(attributes) { + this.createdAt = attributes.createdAt; + this.dimensions = attributes.dimensions; +} + +GameObject.prototype.destroy = function() { + return `Game object was removed from the game.`; +} + +function NPC(npcAttributes) { + GameObject.call(this, npcAttributes); + this.hp = npcAttributes.hp; + this.name = npcAttributes.name; +} + +NPC.prototype = Object.create(GameObject.prototype); + +NPC.prototype.takeDamage = function() { + return `${this.name} took damage.`; +} + +function Humanoid(humanoidAttributes) { + GameObject.call(this, humanoidAttributes); + NPC.call(this, humanoidAttributes); + this.faction = humanoidAttributes.faction; + this.weapons = humanoidAttributes.weapons; + this.language = humanoidAttributes.language; +} + +Humanoid.prototype = Object.create(GameObject.prototype); +Humanoid.prototype = Object.create(NPC.prototype); + +Humanoid.prototype.greet = function() { + return `${this.name} offers a greeting in ${this.language}.`; +} + +// Example: +const hamsterHuey = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + hp: 5, + name: 'Hamster Huey', + faction: 'Gooey Kablooie', + weapons: [ + 'bubblegum', + ], + language: 'Hamsterish', +}); + +console.log(hamsterHuey.greet()); // returns 'Hamster Huey offers a greeting in Hamsterish' +console.log(hamsterHuey.takeDamage()); // returns 'Hamster Huey took damage.' +console.log(hamsterHuey.destroy()); // returns 'Game object was removed from the game.' +// /* eslint-disable no-undef */ module.exports = { GameObject, From ed0e49786556d4908c45f856f88549fe45348333 Mon Sep 17 00:00:00 2001 From: faulkenfett11 <36997363+faulkenfett11@users.noreply.github.com> Date: Thu, 22 Mar 2018 16:48:30 -0700 Subject: [PATCH 2/2] finished up class.js --- src/class.js | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/class.js b/src/class.js index 4a94349..4b999bd 100644 --- a/src/class.js +++ b/src/class.js @@ -7,17 +7,17 @@ // for a potential password that will be compared to the `password` property. // Return true if the potential password matches the `password` property. Otherwise return false. - class User { - constructor() { - this. - this. + constructor(options) { + this.email = options.email; + this.password = options.password; + } + + comparePasswords(userPassword) { + return userPassword === this.password; } } - - - // Part 2 // Create a class called `Animal` and a class called `Cat` using ES6 classes. // `Cat` should extend the `Animal` class. @@ -28,7 +28,32 @@ class User { // `meow` that should return the string ` meowed!` where `` is the `name` // property set on the Cat instance. -// code here +class Animal { + constructor(options) { + this.age = options.age; + } + + growOlder(gettingOlder) { + gettingOlder = this.age + 1; + return older; + } +} + +class Cat extends Animal { + constructor(catOptions) { + super(catOptions); + this.name = name.catOptions; + } + + meow() { + return `${name} meowed!`; + } +} + +const garfield = new Cat({ + name: 'Garfield', + age: 39 +}); /* eslint-disable no-undef */