diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..d3b4fac82 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,7 +7,7 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -16,6 +16,15 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(gamAttributes) { + this.createdAt = gamAttributes.createdAt; + this.name = gamAttributes.name; + this.dimensions = gamAttributes.dimensions; +} +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game`; +} + /* === CharacterStats === * healthPoints @@ -23,6 +32,15 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(charAttributes) { + this.healthPoints=charAttributes.healthPoints; + GameObject.call (this, charAttributes); +} +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.) === * team @@ -32,16 +50,28 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + +function Humanoid(humanAttributes) { + this.team = humanAttributes.team; + this.weapons = humanAttributes.weapons; + this.language = humanAttributes.language; + CharacterStats.call (this, humanAttributes); +} + 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({ createdAt: new Date(), dimensions: { @@ -102,9 +132,9 @@ 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..58b4609fe 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. Window/Global Object Binding +* 2. Implicit Binding +* 3. Explicit Binding +* 4. New Binding * * write out a code example of each explanation above */ @@ -12,15 +12,55 @@ // Principle 1 // code example for Window Binding +function sayName(name) { + console.log(this); + return name; + } + sayName("Reginald Alford"); // Principle 2 - // code example for Implicit Binding +const sayNameFunc = obj => { + obj.sayName = function() { + return `Hello my name is ${this.name}`; + console.log(this); + }; + }; + const me = { name: 'Jason' }; + const you = { name: 'Freddy' }; + sayNameFunc(me); + sayNameFunc(you); + + // Invoke Methods on our objects + console.log(me.sayName()); + console.log(you.sayName()); // Principle 3 - // code example for New Binding - +function CordialPerson(greeter) { + this.greeting = 'Hello '; + this.greeter = greeter; + this.speak = function() { + console.log(this.greeting + this.greeter); + // console.log(this); + }; + } + + const jerry = new CordialPerson('Newman'); + const newman = new CordialPerson('Jerry'); + + jerry.speak(); + newman.speak(); // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +function greet () { + alert(`Hello, my name is ${this.name} and I am ${this.age} years old.`) + } + + const user = { + name: 'Reginald Alford', + age: 38, + } + + greet.call(user); \ No newline at end of file