diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..5c98b4288 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/JS-III.iml b/.idea/JS-III.iml new file mode 100644 index 000000000..c956989b2 --- /dev/null +++ b/.idea/JS-III.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 000000000..e7008fdc7 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..28a804d89 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..57b950a1c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/assignments/index.html b/assignments/index.html index abffdec1a..d2fdf6442 100644 --- a/assignments/index.html +++ b/assignments/index.html @@ -8,7 +8,7 @@ JS III - + diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..3612aa19a 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -15,7 +15,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 (objAttributes) { + this.createdAt = objAttributes.createdAt; + this.name = objAttributes.name; + this.dimensions = objAttributes.dimensions; +} +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game.` +}; /* === CharacterStats === * healthPoints @@ -23,6 +30,15 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(charAttributes) { + GameObject.call(this, charAttributes); + this.healthPoints = charAttributes.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.) === * team @@ -32,7 +48,17 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - + function Humanoid(humanAttributes) { + CharacterStats.call(this, humanAttributes); + this.team = humanAttributes.team; + this.weapons = humanAttributes.weapons; + this.language = humanAttributes.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 * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. @@ -41,7 +67,6 @@ // 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 +127,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..15da10a82 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,46 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Window Binding - When in the global scope, the value of “this” will be the window/console Object +* 2. Implicit Binding - Whenever a function is called by a preceding dot, the object before that dot is this +* 3. New Binding - Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function +* 4. Explicit Binding - Whenever JavaScript’s call or apply method is used, this is explicitly defined * * write out a code example of each explanation above */ // Principle 1 - // code example for Window Binding +console.log(this); // Principle 2 - // code example for Implicit Binding +let theGuy = { + name: 'Guy', + isAction: function (action) { + console.log(`${this.name} is ${action}.`) + } +}; +theGuy.isAction('walking'); // Principle 3 - // code example for New Binding +function Me(attr) { + this.name = attr.name; + this.whoIam = function () { + console.log(`${this.name}'s job is hard.`); + }; +} +let mike = new Me({ + name: 'Mikey', +}); +let john = new Me({ + name: 'John', +}); +mike.whoIam(); +john.whoIam(); // Principle 4 - -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding +john.whoIam.call(mike); +mike.whoIam.call(john); \ No newline at end of file