Skip to content

updated #773

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
41 changes: 39 additions & 2 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 = Object.create(GameObject.prototype);

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



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

function CharacterStats(stats){
GameObject.call(this, stats)
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.) ===
* team
Expand All @@ -32,6 +56,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(attrib){
CharacterStats.call(this, attrib)
this.team = attrib.team,
this.weapons = attrib.weapons,
this.language =attrib.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 +78,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,7 +139,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
73 changes: 65 additions & 8 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,83 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window binding:

here "this" refers to the window or default context

* 2. Implicit binding:

it specifies to the left of "this" to which object it applies or refers to

* 3. Explicit binding:

telling an object what function to use, having call, apply and bind as options. every option has a different behavior

* 4. New Binding:

creating a new object and "this" is the relation between new object and existing objects

*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
const myFunction = function() {
console.log(this.f);
}

var f = 2 ;
myFunction();




// Principle 2

//Principle 2
// code example for Implicit Binding
let myCat = {
name: 'moles',
noise: 'purrrrrr',
cat: function(){
console.log(this.noise);
}
}

myCat.cat();


// Principle 3



// Principle 3
// code example for New Binding

function Activity(movie){
this.time = movie;
}

let myActivity = new Activity('Joker');

console.log(`My next movie is the ${myActivity.time}`);





// Principle 4
// code example for Explicit Binding

function Cooking(){
console.log(this.sauce);
}

let food = {
dish: 'pasta',
sauce: 'alfredo!'
}

letsCook = Cooking.bind(food);


// code example for Explicit Binding
letsCook();