diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..96a3da5f7 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,13 +1,15 @@ /* - Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy. + Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor + functions with their correct inheritance hierarchy. In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid. - At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions. + At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page + to test your constructor functions. Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -15,6 +17,14 @@ * dimensions (These represent the character's size in the video game) * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(attrs) { + this.createdAt = attrs.createdAt + this.name = attrs.name + this.dimensions = attrs.dimensions +} +GameObject.prototype.destroy = function() { + return (`${this.name} was removed from the game.`) +} /* === CharacterStats === @@ -22,6 +32,14 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(character) { + GameObject.call(this, character) + this.healthPoints = character.healthPoints +} +CharacterStats.prototype = Object.create(GameObject.prototype) +CharacterStats.prototype.takeDamage = function() { + return (`${this.name} took damage`) +} /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,79 +50,88 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +function Humanoid(human) { + CharacterStats.call(this, human) + this.team = human.team + this.weapons = human.weapons + this.language = human.language +} +Humanoid.prototype = Object.create(GameObject.prototype) +Humanoid.prototype = Object.create(CharacterStats.prototype) +Humanoid.prototype.greet = function() { + return `${this.name} offers a greeting in ${this.language}` +} + /* - * Inheritance chain: GameObject -> CharacterStats -> Humanoid - * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. - * Instances of CharacterStats should have all of the same properties as GameObject. -*/ + * Inheritance chain: GameObject -> CharacterStats -> Humanoid + * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. + * Instances of CharacterStats should have all of the same properties as GameObject. + */ // Test you work by un-commenting these 3 objects and the list of console logs below: -/* - const mage = new Humanoid({ +const mage = new Humanoid({ createdAt: new Date(), dimensions: { - length: 2, - width: 1, - height: 1, + length: 2, + width: 1, + height: 1 }, healthPoints: 5, name: 'Bruce', team: 'Mage Guild', weapons: [ - 'Staff of Shamalama', + 'Staff of Shamalama' ], - language: 'Common Tongue', - }); + language: 'Common Tongue' +}) - const swordsman = new Humanoid({ +const swordsman = new Humanoid({ createdAt: new Date(), dimensions: { - length: 2, - width: 2, - height: 2, + length: 2, + width: 2, + height: 2 }, healthPoints: 15, name: 'Sir Mustachio', team: 'The Round Table', weapons: [ - 'Giant Sword', - 'Shield', + 'Giant Sword', + 'Shield' ], - language: 'Common Tongue', - }); + language: 'Common Tongue' +}) - const archer = new Humanoid({ +const archer = new Humanoid({ createdAt: new Date(), dimensions: { - length: 1, - width: 2, - height: 4, + length: 1, + width: 2, + height: 4 }, healthPoints: 10, name: 'Lilith', team: 'Forest Kingdom', weapons: [ - 'Bow', - 'Dagger', + 'Bow', + 'Dagger' ], - language: 'Elvish', - }); + language: 'Elvish' +}) - console.log(mage.createdAt); // Today's date - console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } - console.log(swordsman.healthPoints); // 15 - console.log(mage.name); // Bruce - console.log(swordsman.team); // The Round Table - console.log(mage.weapons); // Staff of Shamalama - console.log(archer.language); // Elvish - 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. -*/ +console.log(mage.createdAt); // Today's date +console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } +console.log(swordsman.healthPoints) // 15 +console.log(mage.name) // Bruce +console.log(swordsman.team) // The Round Table +console.log(mage.weapons) // Staff of Shamalama +console.log(archer.language) // Elvish +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. - // * 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 +// Stretch task: +// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function. +// * 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..7de42b72b 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,58 @@ -/* The for principles of "this"; -* in your own words. explain the four principle for the "this" keyword below. -* -* 1. -* 2. -* 3. -* 4. -* -* write out a code example of each explanation above -*/ +/* The for principles of "this" + * in your own words. explain the four principle for the "this" keyword below. + * + * 1. Global Binding + * 2. Implicit Binding + * 3. New Binding + * 4. Explicit Binding + * + * write out a code example of each explanation above + */ // Principle 1 - -// code example for Window Binding +console.log(this) + // code example for Window Binding // Principle 2 // code example for Implicit Binding +const myObj = { + greeting: 'Hello', + sayHello: function(name) { + console.log(`${this.greeting} my name is ${name}`) + console.log(this) + } +} +myObj.sayHello('Ryan') // Principle 3 // code example for New Binding - -// Principle 4 - -// code example for Explicit Binding \ No newline at end of file +function PersonGreeter(greeter) { + this.greeting = 'Hello ' + this.greeter = greeter + this.speak = function() { + console.log(this.greeting + this.greeter) + console.log(this) + } +} + +const lan = new PersonGreeter('lan') +const keisha = new PersonGreeter('keisha') + +lan.speak() +keisha.speak() + // Principle 4 + +// code example for Explicit Binding +function Person(obj) { + this.name = obj.name + this.age = obj.age + this.speak = function() { + console.log(this.name + ' ' + this.age) + } +} +const peter = new Person({ name: 'Peter', age: 60 }) +const samuel = new Person({ name: 'samuel', age: 40 }) +samuel.speak.call(peter) +peter.speak.apply(samuel) \ No newline at end of file