Skip to content
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
26 changes: 14 additions & 12 deletions assignments/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<!doctype html>
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>JS III</title>
<title>JS III</title>

<script src="this.js"></script>
<script src="prototypes.js"></script>
</head>
<script src="this.js"></script>
<script src="prototypes.js"></script>
<script src="inheritance.js"></script>
<script src="parent.js"></script>
</head>

<body>
<h1>JS III - Check your work in the console!</h1>
</body>
</html>
<body>
<h1>JS III - Check your work in the console!</h1>
</body>
</html>
215 changes: 132 additions & 83 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Each constructor function has unique properties and methods that are defined in their block comments below:
*/

/*
=== GameObject ===
* createdAt
Expand All @@ -16,95 +16,144 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(event) {
this.createdAt = event.createdAt;
this.name = event.name;
this.dimensions = event.dimensions;
}

GameObject.prototype.destroy = function() {
return `${this.name} was removed from the game`;
};

const ob = new GameObject({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1
},
name: "Bruce"
});

console.log(ob.destroy());
console.log(ob.createdAt);
/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
* weapons
* language
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* 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.
*/
function CharacterStats(status) {
GameObject.call(this, status);
this.healthPoints = status.healthPoints;
}

// Test you work by un-commenting these 3 objects and the list of console logs below:
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage.`;
};

/*
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
language: 'Common Tongue',
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
});

console.log(mage.createdAt); // Today's date
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(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.
*/
const cs = new CharacterStats({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1
},
healthPoints: 5,
name: "Bruce"
});

console.log(cs.healthPoints);
console.log(cs.takeDamage());

// /*
// === Humanoid (Having an appearance or character resembling that of a human.) ===
// * team
// * weapons
// * language
// * greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
// * should inherit destroy() from GameObject through CharacterStats
// * should inherit takeDamage() from CharacterStats
// */

function Humanoid(traits) {
CharacterStats.call(this, traits);
this.team = traits.team;
this.weapons = traits.weapons;
this.language = traits.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.
// // * 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: {
length: 2,
width: 1,
height: 1
},
healthPoints: 5,
name: "Bruce",
team: "Mage Guild",
weapons: ["Staff of Shamalama"],
language: "Common Tongue"
});

const swordsman = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 2,
height: 2
},
healthPoints: 15,
name: "Sir Mustachio",
team: "The Round Table",
weapons: ["Giant Sword", "Shield"],
language: "Common Tongue"
});

const archer = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 1,
width: 2,
height: 4
},
healthPoints: 10,
name: "Lilith",
team: "Forest Kingdom",
weapons: ["Bow", "Dagger"],
language: "Elvish"
});

console.log(mage.createdAt); // Today's date
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(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.

// 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!
// // 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!
66 changes: 56 additions & 10 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,72 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* write out a code example of each explanation above
*/
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. window binding - bind to the window scope
* 2. implicit binding - when a method is called, this binds to the left of the '.'
* 3. explicit binding - function binds to another object. function binding method (.apply, .bind,.call) needs to be used
* 4. new binding - inheritance/ prototype binding
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function forest(tree) {
console.log(this);
return tree;
}

forest("maple");

// Principle 2

// code example for Implicit Binding

const snowWhite = {
name: "snow white",
live: "the forest",
greeting: function(name) {
console.log(
`Hello ${name}, my name is ${this.name} and I live in ${this.live}.`
);
}
};

snowWhite.greeting("Hobbit");

// Principle 3

// code example for New Binding

function CordialPerson(greeter) {
this.greeting = "Hello";
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + " " + this.greeter);
console.log(this);
};
}

const jerry = new CordialPerson("Newman");
const newman = new CordialPerson("Jerry");

jerry.speak();
newman.speak();

//jerry.speak.call(newman); explicit binding example to newman

// Principle 4

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

const person = {
name: "Naruto",
live: "Konoha"
};

function greetings() {
console.log(`Hello my name is ${this.name} and I live in ${this.live}`);
}

greetings.call(person);