Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ This challenge focuses on using the `this` keyword as well as getting comfortabl

## Git Set up

* [ ] Fork the project into your GitHub user account
* [ ] Clone the forked project into a directory on your machine
* [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [ ] You are now ready to build this project with your preferred IDE
* [x] Fork the project into your GitHub user account
* [x] Clone the forked project into a directory on your machine
* [x] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
* [x] You are now ready to build this project with your preferred IDE

## Assignment Set Up

* Complete all the exercises as described inside each assignment file.
* To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console.
* [x] Complete all the exercises as described inside each assignment file.
* [x] To test your `console.log()` statements, open up the index.html file found in the assignments folder and use the developer tools to view the console.

## The `this` keyword

Expand All @@ -22,5 +22,5 @@ Having a solid understanding of how `this` works will give you a huge advantage

The prototype challenge will focus on building prototypes for a fantasy themed game that includes mages, swordsmen, and archers. Follow the [prototypes.js](assignments/this.js) instructions closely to create the beginnings of what could be an awesome JavaScript game.

* Read the instructions found within the file carefully to finish the challenges.
* Remember to un-comment the objects and console logs to test your work at the bottom of the page.
* [x] Read the instructions found within the file carefully to finish the challenges.
* [x] Remember to un-comment the objects and console logs to test your work at the bottom of the page.
4 changes: 2 additions & 2 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>JS III</title>
<title>Cool Game :)</title>

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

<body>
<h1>JS III - Check your work in the console!</h1>
<h1>Experiments in visualizing JS objects</h1>
</body>
</html>
75 changes: 60 additions & 15 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
Object oriented design is commonly used in video games.
For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.

In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.

At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.
At the bottom of this file are 3 objects that all end up inheriting from Humanoid.
Use the objects at the bottom of the page to test your constructor functions.

Each constructor function has unique properties and methods that are defined in their block comments below:
*/
Expand All @@ -15,6 +17,15 @@
* destroy() // prototype method -> returns the string: 'Object was removed from the game.'
*/

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

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

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

function CharacterStats(gameStats) {
GameObject.call(this, gameStats);
this.healthPoints = gameStats.healthPoints;
this.name = gameStats.name;

}

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,25 +57,41 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/


function Humanoid (gameHumanoid) {
this.team = gameHumanoid.team;
this.weapons = gameHumanoid.weapons;
this.language = gameHumanoid.language;

CharacterStats.call(this, gameHumanoid);
}

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:
// fantasynamegenerators.com

/*
const mage = new Humanoid({
createdAt: new Date(),
createdAt: new Date(), // returns date object, not string
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
healthPoints: 45,
name: 'Algar',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
Expand All @@ -65,11 +106,11 @@
width: 2,
height: 2,
},
healthPoints: 15,
name: 'Sir Mustachio',
healthPoints: 75,
name: 'Winchelll',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Long Sword',
'Shield',
],
language: 'Common Tongue',
Expand All @@ -82,29 +123,33 @@
width: 2,
height: 4,
},
healthPoints: 10,
name: 'Lilith',
healthPoints: 66,
name: 'Kiya',
team: 'Forest Kingdom',
weapons: [
'Bow',
'Dagger',
],
language: 'Elvish',
language: ' Ælvißh',
});

console.log(mage.createdAt); // Today's date
console.log(archer.dimensions); // { length: 1, width: 2, height: 4 }
console.log(swordsman.healthPoints); // 15
console.log(swordsman.healthPoints); // 75
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;
// healthpoints =< 0, .destroy()

// * Create two new objects, one a villain and one a hero and fight it out with methods!
59 changes: 48 additions & 11 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,63 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
/* The four principles of "this";
* in your own words. explain the four principled for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Global
* * Global refers to the global object, in browsers, this is the window object
* 2. Implicit
* * When an object is called by a proceeding dot
* 3. New
* * New refers to the created constructor object instance created and returned by the constructor function `const a new someFunc('Newobj');`
* 4. Explicit
* * using .call, .apply methods .call for argument list .apply for single array of args
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
console.log(this === window); // should return true :thinking:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does :)


// Principle 2

// code example for Implicit Binding
const myObj = {
greeting: 'Hello',
sayHello: function(name) {
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}
};
myObj.sayHello('Ryan');
// lifted straight from learn page^

// 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'); // new binding!
const newman = new CordialPerson('Jerry'); // new binding!

jerry.speak();
newman.speak();
// the functions arguement is being returned by the called function with `this`

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function Product(name, price) {
this.name = name;
this.price = price;
}

function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}

console.log(new Food('cheese', 5).name);
// expected output: "cheese"