diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..166607a15 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -8,13 +8,14 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ -/* - === GameObject === - * createdAt - * name - * 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 (mainat) { + this.createdAt = mainat.createdAt; + this.dimensions = mainat.dimensions; + this.name = mainat.name; +}; + GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.`; + }; /* === CharacterStats === @@ -22,6 +23,14 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(CharInfo) { + GameObject.call(this, CharInfo); + this.healthPoints = CharInfo.healthPoints; + this.name = CharInfo.name; +} +CharacterStats.prototype.takeDamage = function() { + return `${this.name} took damage`; +}; /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,6 +41,20 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ +function Humanoid(player1) { + CharacterStats.call(this, player1); + this.team = player1.team; + this.weapon = player1.weapon; + this.language = player1.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 +64,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 +125,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..731b6e1f2 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,65 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. New binding is when a new variable creates an object and the "this" keyword directs to it +* 2. Window Binding is when no other rules are present the "this" keyword will automatically implement window object, if strict mode is active it will default undefined instead. +* 3. Implicit Binding is when a function is given the keyword "this" to allow invoking. +* 4. Explicit Binding is when we descriptivly tell the engine to use call, apply, or bind when pointing to a specific value, these three words can invoke fuctions with certain values of "this" * * write out a code example of each explanation above */ // Principle 1 +function hotWheel(monster){ + console.log(`I use to love` + `${this.monster}` + ` trucks`); + return monster; + +} +hotWheel(this); // code example for Window Binding // Principle 2 +let jay = { + name: "Jarrod", + says: "what's up guys!", + jaySays: function (){ + console.log(this.says) + } +} + +jay.jaySays(); + + // code example for Implicit Binding -// Principle 3 -// code example for New Binding // Principle 4 +function car(){ + console.log(this.fast); +} + +let wrx = { + brand: "subaru", + fast: "0-60 in 5.5 seconds" +} + +car.call(wrx) + + + +// code example for Explicit Binding + +// Principle 3 +function fastCar(brz){ + this.thing = brz; +} + +let Car = new fastCar('brz goes 0-60 in 6.2 sec'); +console.log(Car.thing); + + -// code example for Explicit Binding \ No newline at end of file +// code example for New Binding \ No newline at end of file