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/prototypes.js b/assignments/prototypes.js index f878edd25..2926c9c97 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -19,12 +19,15 @@ 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 === * healthPoints @@ -32,13 +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 @@ -49,16 +55,18 @@ function CharacterStats(attribute) { * should inherit takeDamage() from CharacterStats */ 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}` - } - GameObject.call(this, attribute); - 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. diff --git a/assignments/this.js b/assignments/this.js index 9709a50db..6c94cda3c 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 */ @@ -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