diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..75110862d 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -1,12 +1,13 @@ /* 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. Each constructor function has unique properties and methods that are defined in their block comments below: */ + /* === GameObject === @@ -15,6 +16,15 @@ * 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(gameobj){ + this.createdAt= gameobj.createdAt; + this.name= gameobj.name; + this.dimensions= gameobj.dimensions; + +} + GameObject.prototype.destroy= function(){ + return `${this.name} was remove from the page`; + } /* === CharacterStats === @@ -22,6 +32,14 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(startObj){ + GameObject.call(this,startObj); + this.healthPoints= startObj.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,7 +50,17 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + function Humanoid(hummanObj){ + CharacterStats.call(this,hummanObj); + this.team=hummanObj.team; + this.weapons=hummanObj.weapons; + this.language=hummanObj.language; + + } + Humanoid.prototype=Object.create(CharacterStats.prototype); + Humanoid.prototype.greet=function(){ + return `${this.name} speak greet in ${this.language} .`; + } /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. @@ -41,7 +69,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,7 +130,7 @@ 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. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..ddaff5e04 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,24 +1,50 @@ -/* The for principles of "this"; +0/* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. +* 1. global scope is a window +* 2. impliciti bilding +* 3. new * 4. * * write out a code example of each explanation above */ // Principle 1 +function sing(singer) { + console.log(this); + return name; + } + sing(); // code example for Window Binding -// Principle 2 +// Principle 2 +const myCar = { + star: 'tur on', + secondStep: function( pushAc) { + console.log(`${this.star} im ready ${pushAc}`); + console.log(this); + } + }; + myCar.secondStep('Go'); // code example for Implicit Binding // Principle 3 - +const makeOrder = obj => { + obj.sayName = function() { + console.log(`Hello my name is ${this.name}`); + console.log(this); + }; + }; + const me = { name: 'Ryan' }; + const you = { name: 'Freddy' }; + sayNameFunc(me); + sayNameFunc(you); + + // Invoke Methods on our objects + me.sayName(); + you.sayName(); // code example for New Binding // Principle 4