diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..89576d433 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,17 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(attr){ + + this.createdAt = attr.createdAt; + this.name = attr.name; + this.dimensions = attr.dimensions; + +} +GameObject.prototype.destroy = function(){ + return `${this.name} was removed from the game.` +} + /* === CharacterStats === * healthPoints @@ -23,6 +34,16 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(attr){ + GameObject.call(this,attr); + this.healthPoints = attr.healthPoints; +} +CharacterStats.prototype = Object.create(GameObject.prototype); +CharacterStats.prototype.takeDamage = function(){ + return `${this.name} took damages` +} + + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -32,6 +53,20 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ + +function Humanoid(attr){ + CharacterStats.call(this,attr); + this.team = attr.team; + this.weapons = attr.weapons; + this.language = attr.language; + +} +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function(){ +return `${this.name} offers a greeting in ${this.language}` +} + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid @@ -41,7 +76,7 @@ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* + const mage = new Humanoid({ createdAt: new Date(), dimensions: { @@ -102,9 +137,33 @@ console.log(archer.greet()); // Lilith offers a greeting in Elvish. console.log(mage.takeDamage()); // Bruce took damage. console.log(swordsman.destroy()); // Sir Mustachio was removed from the game. -*/ + // Stretch task: - // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. + // * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. + function Villain(attr){ + Humanoid.call(this, attr); + } + Villain.prototype = Object.create(Humanoid.prototype) + + Villain.prototype.removeHealth = function(ptReduce){ + this.healthpoints = this.healthPoints - ptReduce; + if (this.healthpoints <= 0) return this.destroy(); + } + + function Hero(attr){ + Humanoid.call(this, attr); + } + + Hero.prototype = Object.create(Humanoid.prototype); + + + Hero.prototype.removeHealth = function(ptReduce){ + this.healthpoints = this.healthPoints - ptReduce; + if (this.healthpoints <= 0) return this.destroy(); + } + + // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; + // * Create two new objects, one a villain and one a hero and fight it out with methods! \ No newline at end of file diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..df7063939 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,10 +1,10 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. global bind: without giving the context, this is binding to window ojbects +* 2. implicit binding: when we call object methods, this is bind to the object to the left of dot +* 3. explicit binding: we can bind this with call, apply, bind methods by explicitly specifing the object +* 4. new binding: when we create an object with new keyword, which were formatted by function contructor, this is bind to the object intantiated by new keyword * * write out a code example of each explanation above */ @@ -13,14 +13,70 @@ // code example for Window Binding + +// function test(){ +// console.log(this); + +// } +// test(); + // Principle 2 // code example for Implicit Binding +// const animal = { +// name: 'cat', +// age: 5, +// origin: 'china', +// remarks: function(){ +// return `this ${this.name} of age ${this.age} is from ${this.origin}` +// } + +// } + +// console.log(animal.remarks()); // Principle 3 // code example for New Binding +// function Person(attributes){ +// this.name = attributes.name; +// this.age = attributes.age; +// this.origin = attributes.origin; + + +// } + +// Person.prototype.speak = function(){ +// return `my name is ${this.name}, age of ${this.age}, and from ${this.origin}` +// } + +// const james = new Person({ +// name: 'james', +// age: 25, +// origin: 'Japan' + +// }); + +// console.log(james.speak()); + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +// const animal = { +// name: 'cat', +// age: 5, +// origin: 'china', +// remarks: function(greeting){ +// return `${greeting} ${this.name} of age ${this.age} is from ${this.origin}` +// } + +// } + +// const person = { +// name: 'james', +// age: 35, +// origin: 'USA' +// } + +// console.log(animal.remarks.call(person, 'hello'));