From 8dac53754838f6b378670f1bfff8b028c489a80a Mon Sep 17 00:00:00 2001 From: Blaque Date: Wed, 16 Oct 2019 16:31:58 -0400 Subject: [PATCH 1/5] answered questions --- assignments/this.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/assignments/this.js b/assignments/this.js index 9709a50db..c4e02d7df 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. in a global scope this is refering to the window or console object -* 2. implicit binding is refering to the object you are currently working on -* 3. when using a new constuctor binding the this keyword is refering to the object being created and returned -* 4. in explicit binding the this keyword is defined directly +* 1. The keyword This on a global scope refers to the window or console object +* 2. The keyword This in implicit binding refers to the object to left of the kewword This. +* 3. The keyword This when used with the keyword New in a constuctor binding refers to the object being created and returned +* 4. The keyword this in explicit binding refers to the parameter being added. * * write out a code example of each explanation above */ From 21783299bad9936430ba10e9a5434f137c43f2d3 Mon Sep 17 00:00:00 2001 From: Blaque Date: Wed, 16 Oct 2019 17:08:59 -0400 Subject: [PATCH 2/5] this.js file working --- .vscode/settings.json | 3 +++ assignments/this.js | 35 ++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..f673a71b7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file diff --git a/assignments/this.js b/assignments/this.js index c4e02d7df..6c94cda3c 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -12,20 +12,15 @@ // Principle 1 // code example for Window Binding -function sayHi(greeting) { - console.log(this); - return greeting; -} -sayHi("Hello") +console.log(this) // Principle 2 // code example for Implicit Binding const person = { name: "John", - sayName: function(name) { + sayName: function() { console.log(`${this.name}`); - console.log(this); } } person.sayName(); @@ -37,14 +32,28 @@ function MorningGreeting(person) { this.greeter = person; this.tell = function() { console.log(`${this.greeting} ${this.greeter}`); - console.log(this); + } } -const maria = new MorningGreeting("Maria"); -console.log(maria.tell()); +const goodMorningMaria = new MorningGreeting("Maria"); +goodMorningMaria.tell() +console.log(goodMorningMaria) + // Principle 4 // code example for Explicit Binding -const kim = new MorningGreeting("Kim") -console.log(kim.tell.call(maria)); -console.log(kim.tell()); +function chores(person) { + console.log(`It's ${this.name}'s turn to do the ${this.chore}`) +} + +const kid1 = { + name: "Tim", + chore: "dishes" +} + +const kid2 = { + name: "Mary", + chore: "mopping" +} +chores.call(kid1); +chores.call(kid2) \ No newline at end of file From 4f40c632ddaedf85c9c6c78d1e8b8327f6338cf6 Mon Sep 17 00:00:00 2001 From: Blaque Date: Wed, 16 Oct 2019 17:36:11 -0400 Subject: [PATCH 3/5] cant get prototype to work --- assignments/prototypes.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index f878edd25..bdd8a3999 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -19,11 +19,9 @@ function GameObject(attribute) { this.createdAt = attribute.createdAt; this.name = attribute.name; this.dimensions = attribute.dimensions; - this.destroy = function(){ - return `${this.name} was removed from the game`; - } - - + this.destroy = function() { + return `${this.name} was removed from the game`; + } } /* === CharacterStats === @@ -32,13 +30,13 @@ function GameObject(attribute) { * should inherit destroy() from GameObject's prototype */ function CharacterStats(attribute) { + this.healthPoints = attribute.healthPoints; this.takeDamage = function() { return `${attribute.name} took damage` } GameObject.call(this, attribute); - -} + } /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -49,14 +47,17 @@ function CharacterStats(attribute) { * should inherit takeDamage() from CharacterStats */ function Humanoid(attribute) { + + this.team = attribute.team; this.weapons = attribute.weapons; this.language = attribute.language; this.greet = function() { return `${attribute.name} offers a greeting in ${this.language}` } - GameObject.call(this, attribute); + GameObject.call(this, attribute); CharacterStats.call(this, attribute); + } /* From 5131555c23e10a68ee23ffe0d35e3ac53edd5254 Mon Sep 17 00:00:00 2001 From: Blaque Date: Wed, 16 Oct 2019 18:14:13 -0400 Subject: [PATCH 4/5] removed extra code --- assignments/prototypes.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index bdd8a3999..b52cd1149 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -20,9 +20,12 @@ function GameObject(attribute) { this.name = attribute.name; this.dimensions = attribute.dimensions; this.destroy = function() { - return `${this.name} was removed from the game`; - } + return `${this.name} was removed from the game`; + } + } + + /* === CharacterStats === * healthPoints @@ -36,7 +39,8 @@ function CharacterStats(attribute) { return `${attribute.name} took damage` } GameObject.call(this, attribute); - } +} + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -55,7 +59,6 @@ function CharacterStats(attribute) { this.greet = function() { return `${attribute.name} offers a greeting in ${this.language}` } - GameObject.call(this, attribute); CharacterStats.call(this, attribute); From a07be81915038045617adc342d77c13473da4019 Mon Sep 17 00:00:00 2001 From: Blaque Date: Wed, 16 Oct 2019 20:28:27 -0400 Subject: [PATCH 5/5] added object.create to inherit prototypes --- assignments/prototypes.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/assignments/prototypes.js b/assignments/prototypes.js index b52cd1149..2926c9c97 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -19,12 +19,14 @@ function GameObject(attribute) { this.createdAt = attribute.createdAt; this.name = attribute.name; this.dimensions = attribute.dimensions; - this.destroy = function() { - return `${this.name} was removed from the game`; - } + // this.destroy = function() { + // return `${this.name} was removed from the game`; + // } } - +GameObject.prototype.destroy = function() { + return `${this.name} was removed from the game`; +}; /* === CharacterStats === @@ -33,14 +35,16 @@ function GameObject(attribute) { * should inherit destroy() from GameObject's prototype */ function CharacterStats(attribute) { - - this.healthPoints = attribute.healthPoints; - this.takeDamage = function() { - return `${attribute.name} took damage` - } GameObject.call(this, attribute); + this.healthPoints = attribute.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 @@ -52,17 +56,17 @@ function CharacterStats(attribute) { */ function Humanoid(attribute) { - + CharacterStats.call(this, attribute); this.team = attribute.team; this.weapons = attribute.weapons; this.language = attribute.language; - this.greet = function() { - return `${attribute.name} offers a greeting in ${this.language}` - } - CharacterStats.call(this, attribute); - + } + +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.