Skip to content

John Nweke #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
* 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 (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.`;
};

//TEST
//console.log(swordsman.destroy());

/*
=== CharacterStats ===
Expand All @@ -23,6 +35,17 @@
* should inherit destroy() from GameObject's prototype
*/

function CharacterStats (attributes) {
//.Call Method
GameObject.call(this, attributes);
this.healthPoints = attributes.healthPoints;
}
//Inheriting Game Object
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
Expand All @@ -32,16 +55,51 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid (attributes) {
//.call Method
CharacterStats.call(this, attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.language;
}

//Inheriting Character Object
Humanoid.prototype = Object.create(CharacterStats.prototype);

Humanoid.prototype.greet = function () {
return `${this.name} offers a greeting in ${this.language}.`
};

//VILLAIN CONSTRUCTOR (Stretch Task)
function Villain (attributes) {
// .call Method - calls the object you want to pull from
Humanoid.call(this, attributes);
this.goodOrEvil = attributes.goodOrEvil;
}

//Inheriting Humanoid Object - references the current function and links to the function you want to pull from
Villain.prototype = Object.create(Humanoid.prototype);

//HERO CONSTRUCTOR (Stretch Task)
function Hero (attributes) {
// .call Method
Humanoid.call (this, attributes);
this.goodOrEvil = attributes.goodOrEvil;
}

//Inheriting Humanoid Object
Hero.prototype = Object.create(Humanoid.prototype);


/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Inheritance chain: GameObject -> CharacterStats -> Humanoid -> Villain & Hero
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
* Instances of CharacterStats should have all of the same properties as GameObject.
*/

// Test you work by un-commenting these 3 objects and the list of console logs below:

/*

const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
Expand All @@ -56,6 +114,7 @@
'Staff of Shamalama',
],
language: 'Common Tongue',

});

const swordsman = new Humanoid({
Expand All @@ -73,6 +132,7 @@
'Shield',
],
language: 'Common Tongue',
goodOrEvil: 'Evil',
});

const archer = new Humanoid({
Expand Down Expand Up @@ -102,7 +162,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.
Expand Down
60 changes: 55 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -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 Object Binding/Global Object Binding: In the global scope, the value of 'this' is the window object.
* 2. Implicit Binding: When a function is called using dot notation, the value of 'this' is the object to the left of the dot (or preceding the dot).
* 3. New Binding: When a constructor function is used (with the 'new' keyword), the value of 'this' is the specific instance of the object created by the constructor.
* 4. Explicit Binding: When JS .call or .apply methods are used, the value of 'this' is explicitly defined using .call, .apply, or .bind
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,64 @@

// code example for Window Binding

function callMyPhone (number) {
console.log (this); //Outputs my entire Window Object!
return number;
}
//TEST
//callMyPhone(`NumberOne`);

// Principle 2

// code example for Implicit Binding
const typeOfPhone = {
phoneType: 'Samsung',
isIphone: false,
explain: function(phone) {
console.log(`John had a ${phone}, but now he uses a ${this.phoneType}.`);
console.log(this);
}
};
//TEST
typeOfPhone.explain(`iPhone 5`);

// Principle 3

// code example for New Binding

//Constructor Function
function Car (brand, model, year) {
this.brand = brand;
this.model = model;
this.year = year;
// this.print = function () {
// console.log(`John is getting a ${this.year} ${this.brand} ${this.model} before the end of the year.`);
// console.log(this);
// };
}
//Make a Prototype function for Car
Car.prototype.print = function () {
console.log(`John is getting a ${this.year} ${this.brand} ${this.model} before the end of the year.`);
console.log(this);
};

//New Items from Constructor
const kia = new Car ('Kia', 'Niro', '2019');
const jeep = new Car ('RangeRover', 'Sport', '2020');
const mazda = new Car ('Mazda', 'CX-9', '2020');

//Call Constructor Method print
kia.print();
jeep.print();
mazda.print();



// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding

//Overriding the New items from above using call and apply
kia.print.call(mazda);
jeep.print.apply(kia);
mazda.print.bind(jeep);