From d4a58463093b6847a05f4f59981b385f1d472429 Mon Sep 17 00:00:00 2001 From: zakabdi Date: Wed, 24 Feb 2021 19:01:54 -0500 Subject: [PATCH] i did it late --- classes.js | 56 ++++++++++++++++++++++++++++++++- prototypes.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++---- this.js | 53 ++++++++++++++++++++++++++++---- 3 files changed, 181 insertions(+), 13 deletions(-) diff --git a/classes.js b/classes.js index 1b13e77..696f380 100644 --- a/classes.js +++ b/classes.js @@ -7,4 +7,58 @@ // Also give it a method "Speak", which says "Hello, My name is + name + and I'm + age + years old. I love + Hobby" -// Extend Person class and create one class for each member of your family. \ No newline at end of file +// Extend Person class and create one class for each member of your family. + + +class Person { + constructor(familyMember){ + this.name = familyMember.name; + this.Age = familyMember.Age; + this.Hobby = familyMember.Hobby; + } + + Speak (){ + return `Hello, my name is ${this.name} and I am ${this.Age } years old. I love ${this.Hobby}` + } +} + +class Child extends Person{ + constructor(children){ + super(children) + + } +} + + +const dad = new Person ({ + name:'abdi', + Age:46, + Hobby:"Reading Qurran" +}); + +const FirstSon = new Child ({ + name:'Zakaria', + Age:23, + Hobby:"Reading Qurran" +}); + +const secondSon = new Child ({ + name:'abdirashid', + Age:22, + Hobby:"Reading Qurran" +}); +const firstDaughter = new Child ({ + name:'ayaan', + Age:12, + Hobby:"Reading Qurran" +}); + + + + + + +console.log(dad.Speak()); +console.log(FirstSon.Speak()); +console.log(secondSon.Speak()); +console.log(firstDaughter.Speak()); diff --git a/prototypes.js b/prototypes.js index 5625c97..121c44a 100644 --- a/prototypes.js +++ b/prototypes.js @@ -16,6 +16,28 @@ * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ + +function GameObject (attributes){ + + this.createdAt = attributes.createdAt; + this.name = attributes.name; + this.dimensions = attributes.dimensions; + + + +} + + +GameObject.prototype.destroy = function (){ + return `${this.name} was removed from the game.`; + } + + + + + + + /* === CharacterStats === * healthPoints @@ -23,6 +45,22 @@ * should inherit destroy() from GameObject's prototype */ + +function CharacterStats (charaterAtrbute){ + GameObject.call(this, charaterAtrbute); + + this.healthPoints= charaterAtrbute.healthPoints; +} + + +CharacterStats.prototype.takeDamage = function (){ + return `${this.name} took damage.`; +} + + + + + /* === Humanoid (Having an appearance or character resembling that of a human.) === * team @@ -32,6 +70,19 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ +function Humanoid (HumanoidAttribute){ + CharacterStats.call(this, HumanoidAttribute); + + this.team = HumanoidAttribute.team; + this.weapons = HumanoidAttribute.weapons; + this.language = HumanoidAttribute.language; + +} + +Humanoid.prototype.greet = function(){ + return `${this.name} offers a greeting in ${this.language}.`; +} + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid @@ -41,7 +92,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: { @@ -96,15 +147,37 @@ console.log(archer.dimensions); // { length: 1, width: 2, height: 4 } console.log(swordsman.healthPoints); // 15 console.log(mage.name); // Bruce - console.log(swordsman.team); // The Round Table + console.log(swordsman.team); // The Round Table console.log(mage.weapons); // Staff of Shamalama console.log(archer.language); // Elvish 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. -*/ + 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. // * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0; - // * Create two new objects, one a villain and one a hero and fight it out with methods! \ No newline at end of file + // * Create two new objects, one a villain and one a hero and fight it out with methods! + + + diff --git a/this.js b/this.js index 969bbee..efcee41 100644 --- a/this.js +++ b/this.js @@ -1,10 +1,13 @@ -/* The for principles of "this"; +/* +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 = "this" keyword tells us where and what it points which is usually being an in the window object. +* 2. Implicit Binding ="look at the left of the dot which calls for the referrence object. 80% usage in the web Devement, and it should use it on objects with methods" +* 3. Explicit Binding = it uses "call" in most cases to connect the two thing, it rarely be used, it immediately and automatically invokes the fucntion. it takes argument. +* 4. new Binding = it uses the "new" keyword and it creates the new object from constructed object and poinst to it. * * write out a code example of each explanation above */ @@ -12,15 +15,53 @@ // Principle 1 // code example for Window Binding +function logThis() { + console.log( this) + } + // Principle 2 // code example for Implicit Binding +var myInfo = { + name:"zack", + Age:23, + sayMyInfo: function(){ + console.log(this.name); + } +}; + myInfo.sayMyInfo();//outpu = "zack" // Principle 3 // code example for New Binding +function States (name, location){ + this.name = name; + this.location =location; + +} +const stateOne = new States("ohio", "USA"); +console.log(stateOne.location); +console.log(stateOne.name); + + + // Principle 4 -// code example for Explicit Binding \ No newline at end of file +// code example for Explicit Binding + +function person (){ + console.log(this.name); +} + +const personOne = { + name:"abdi" +} + + +const personTwo = { + name:"mahdi" +} + +person.call(personOne) \ No newline at end of file