diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..2a2bd5ab7 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -16,6 +16,16 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(attribute){ + this.createdAt = attribute.createdAt; + this.name = attribute.name; + this.dimensions = attribute.dimensions; +} + +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game` +}; + /* === CharacterStats === * healthPoints @@ -23,6 +33,17 @@ * should inherit destroy() from GameObject's prototype */ +function CharacterStats(attribute) { + 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 @@ -33,6 +54,19 @@ * should inherit takeDamage() from CharacterStats */ +function Humanoid(attribute) { + CharacterStats.call(this, attribute); + this.team = attribute.team; + this.weapons = attribute.weapons; + this.language = attribute.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. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..6a8210caa 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. -* 2. -* 3. -* 4. +* 1. Window or Global binding. This means if used on the global scope (not inside a function or method) then it will be pointed at the Global scope. +* 2. Implicit binding. When you call a function using a dot, then the this applies to the function mentioned on the left side of that dot. That's the function we'll be using. +* 3. Explicit binding. When you call a function and use .call, .apply, or bind. You "explicitly" tell "this" to use the function mentioned in the argument. +* 4. New Biding. When a constructor function is used. "This" is used in the object to refer to a specific item and then you can return that in the constructor function. * * write out a code example of each explanation above */ @@ -13,14 +13,56 @@ // code example for Window Binding -// Principle 2 +funtion animalName(type) { + console.log(this); + return type; +} +animalName('panther'); + +// Principle 2 // code example for Implicit Binding +const myMonster = { + name: 'Dracula', + hobby: 'suck blood', + bat: '🦇', + vampire: function(){ + console.log(`Hi, I'm ${this.name} and I like to ${this.hobby}. At night, I fly in the form of a ${this.bat}`); + } +} + +myMonster.vampire(); + +// // Principle 3 +// // code example for New Binding + +function Travel(country) { + this.place = country; +} + +let planningTravel = new Travel('Egypt'); +console.log(`When I graduate Lambda, the first place I plan to visit is ${planningTravel.place}!`); + + +// // Principle 4 + +// // code example for Explicit Binding -// Principle 3 +function myMusic(){ + console.log(`I love music! My favorite singer is ${this.name}. Their style is ${this.style} and my favorite song is ${this.song}!`); +} -// code example for New Binding +let smEnt = { + name: 'EXO', + style: 'Kpop', + song: 'Call me Baby' +} -// Principle 4 +let rnb = { + name: 'Lizzo', + style: 'Soul,R&B,rap', + song: 'Soulmate' +} -// code example for Explicit Binding \ No newline at end of file +myMusic.call(smEnt); +myMusic.call(rnb); \ No newline at end of file