diff --git a/assignments/Constructor Functions.js b/assignments/Constructor Functions.js new file mode 100644 index 000000000..36e7d2587 --- /dev/null +++ b/assignments/Constructor Functions.js @@ -0,0 +1,64 @@ +Interitance in Constructors +Step One - create the Object based on key: value pairs needed for the project. + Example- + const Fred = ({ + age: 45, + name: "Fred", + location: "Bedrock", + phrase: "Yabba dabba do" + }); + +Step Two - create the constructor called Parent. + Example - + function Parent(attributes) { + this.newAge = attributes.age; + this.newName = attributes.name; + this.newLocation = attributes.location; + this.newPhrase= attributes.phrase; + console.log(this); + } +Step Three - if we want to add a a key: value to the Object or Parent, we have to add it to the _proto_ using: + Example - + Parent.prototype.speak = function() { + return `${this.newName} says ${this.newPhrase}`; + } + +Step Four - Use explicit binding to connect or transfer this: + function Parent(attributes) { + this.newAge = attributes.age; + this.newName = attributes.name; + this.newLocation = attributes.location; + this.newPhrase= attributes.phrase; + console.log(this); + } + + ...to this. Using the Parent.call method, allows us bind these together to create DRY coding, as to prevent us from + needing to retype the Object again. + + function Child(childAttributes) { + + console.log(childAttributes); + this.newToy = childAttributes.toy; + }; + + + + + + + ----------------- + const mage = new Humanoid({ + createdAt: new Date(), + dimensions: { + length: 2, + width: 1, + height: 1, + }, + healthPoints: 5, + name: 'Bruce', + team: 'Mage Guild', + weapons: [ + 'Staff of Shamalama', + ], + language: 'Common Tongue', + }); \ No newline at end of file diff --git a/assignments/index.html b/assignments/index.html index abffdec1a..0f02d9171 100644 --- a/assignments/index.html +++ b/assignments/index.html @@ -11,7 +11,7 @@ - +

JS III - Check your work in the console!

- \ No newline at end of file + \ No newline at end of file diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..d89674966 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -14,7 +14,16 @@ * 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(objects) { + this.createdAt = objects.createdAt; + this.name = objects.name; + this.dimensions = objects.dimensions; +} + GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game.`; + }; + /* === CharacterStats === @@ -22,6 +31,16 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(stats) { + GameObject.call(this, stats); + this.healthPoints = stats.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,6 +51,16 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ + function Humanoid(attributes) { + CharacterStats.call(this, attributes); + this.team = attributes.team; + this.weapons = attributes.weapons; + this.language = attributes.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 +70,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: { @@ -68,10 +97,7 @@ healthPoints: 15, name: 'Sir Mustachio', team: 'The Round Table', - weapons: [ - 'Giant Sword', - 'Shield', - ], + weapons: ['Giant Sword', 'Shield',], language: 'Common Tongue', }); @@ -102,7 +128,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.