Skip to content

Javascript-III #771

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 1 commit 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
65 changes: 62 additions & 3 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
*/

function GameObject(attr){

this.createdAt = attr.createdAt;
this.name = attr.name;
this.dimensions = attr.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(attr){
GameObject.call(this,attr);
this.healthPoints = attr.healthPoints;
}
CharacterStats.prototype = Object.create(GameObject.prototype);
CharacterStats.prototype.takeDamage = function(){
return `${this.name} took damages`
}


/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand All @@ -32,6 +53,20 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attr){
CharacterStats.call(this,attr);
this.team = attr.team;
this.weapons = attr.weapons;
this.language = attr.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
Expand All @@ -41,7 +76,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 +137,33 @@
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.
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
function Villain(attr){
Humanoid.call(this, attr);
}
Villain.prototype = Object.create(Humanoid.prototype)

Villain.prototype.removeHealth = function(ptReduce){
this.healthpoints = this.healthPoints - ptReduce;
if (this.healthpoints <= 0) return this.destroy();
}

function Hero(attr){
Humanoid.call(this, attr);
}

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


Hero.prototype.removeHealth = function(ptReduce){
this.healthpoints = this.healthPoints - ptReduce;
if (this.healthpoints <= 0) return this.destroy();
}


// * 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: 61 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. global bind: without giving the context, this is binding to window ojbects
* 2. implicit binding: when we call object methods, this is bind to the object to the left of dot
* 3. explicit binding: we can bind this with call, apply, bind methods by explicitly specifing the object
* 4. new binding: when we create an object with new keyword, which were formatted by function contructor, this is bind to the object intantiated by new keyword
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,70 @@

// code example for Window Binding


// function test(){
// console.log(this);

// }
// test();

// Principle 2

// code example for Implicit Binding

// const animal = {
// name: 'cat',
// age: 5,
// origin: 'china',
// remarks: function(){
// return `this ${this.name} of age ${this.age} is from ${this.origin}`
// }

// }

// console.log(animal.remarks());
// Principle 3

// code example for New Binding

// function Person(attributes){
// this.name = attributes.name;
// this.age = attributes.age;
// this.origin = attributes.origin;


// }

// Person.prototype.speak = function(){
// return `my name is ${this.name}, age of ${this.age}, and from ${this.origin}`
// }

// const james = new Person({
// name: 'james',
// age: 25,
// origin: 'Japan'

// });

// console.log(james.speak());

// Principle 4

// code example for Explicit Binding
// code example for Explicit Binding
// const animal = {
// name: 'cat',
// age: 5,
// origin: 'china',
// remarks: function(greeting){
// return `${greeting} ${this.name} of age ${this.age} is from ${this.origin}`
// }

// }

// const person = {
// name: 'james',
// age: 35,
// origin: 'USA'
// }

// console.log(animal.remarks.call(person, 'hello'));