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
2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
<body>
<h1>JS III - Check your work in the console!</h1>
</body>
</html>
</html>
51 changes: 42 additions & 9 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
/*
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.
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.

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

/*
=== GameObject ===
* createdAt
* name
* 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 (persona){
this.createdAt = persona.createdAt;
this.name = persona.name;
this.dimensions = persona.dimensions;
}
GameObject.prototype.destroy = function()
{
return (`${this.name} was removed from the game.`)

}

/*
=== CharacterStats ===
* healthPoints
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
* should inherit destroy() from GameObject's prototype
*/
function CharacterStats(stats){
GameObject.call(this,{name : stats.name, dimensions : stats.dimensions, createdAt : stats.createdAt})
this.healthPoints = stats.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.) ===
Expand All @@ -32,7 +52,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(character)
{
CharacterStats.call(this,{healthPoints : character.healthPoints,name : character.name,dimensions : character.dimensions, createdAt : character.createdAt})
this.team = character.team
this.weapons = character.weapons
this.language = character.language


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done setting up the constructor functions and prototypes! 💯

/*
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
Expand All @@ -41,7 +74,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: {
Expand Down Expand Up @@ -102,9 +135,9 @@
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.

// 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!
// * Create two new objects, one a villain and one a hero and fight it out with methods!
53 changes: 48 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,69 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Global - this will call the window of the browser because the binding of this is Global

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Generally, it is the "global object". Whatever that is depends on the environment. On browsers, it indeed is window, but in Node.js, a runtime environment used to run JS outside the browser, the global object is console.

* 2. Implicit - this will call the object to the left of the "." where the function is called

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Slightly better wording is that this would be whatever the object is left of the dot. What is being "called" is the method (a function which is a property of an object).

* 3. New - its used for constructors. This will call the object being created by the constructor when it is called

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Terminology is probably "bind" for this, and "call" for functions

* 4. Explicit - Its used to specify to this what exactly to call
*
* write out a code example of each explanation above
*/

// Principle 1
function globalisation(){
console.log(this)
}
globalisation();

// code example for Window Binding

// Principle 2

// code example for Implicit Binding
const obj1 = {
name:"Sachin" ,
englishpremier: function()
{
console.log (`${this.name} love the english premier league`)
}
}
obj1.englishpremier()

// Principle 3

// code example for New Binding
function CreateHuman(humanone){
this.name = humanone.name
this.gender = humanone.gender
this.garden = humanone.garden
}
CreateHuman.prototype.hello = function() {
return(`${this.name} from ${this.garden} says hello`)
}

const human = new CreateHuman(
{name: 'Adam',
gender: 'M',
garden: 'Eden'}
)
console.log(human.hello())
// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
function Inventhuman(attributes){
this.namej = attributes.namej
this.genderj = attributes.genderj
this.gardenj = attributes.gardenj
}

function Married(name1, name2)
{
Inventhuman.apply(this,name1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you use apply, the second argument must be an array of arguments:

Suggested change
Inventhuman.apply(this,name1)
Invent.human.apply(this, [name1])

Alternatively, call works as well:

    Inventhuman.call(this, name1);

MDN Reference

this.name2 = name2
}
Married.prototype = Object.create(Inventhuman.prototype)
Married.prototype.whosmarried = function()
{`${this.name} is from ${this.garden} and is married to ${this.name2}`}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remember, if you want an output from a function/method, what keyword must be present in the body of the function?

Right now, we're getting a big fat undefined from the console log. Anything in this line cluing you in to why?


const marriage = new Married ({namej:'Adam',genderj:'M', gardenj:'Eden'}, 'Eve')
console.log(marriage.whosmarried())