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


function GameObject(createdAt, name, dimensions) {
this.createdAt = createdAt;
this.name = name;
// 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(name, healthpoints){
this.name = name;
}

CharacterStats.prototype.takeDamage = function() {
return `${this.name} took damage`;
};

/*
=== Humanoid (Having an appearance or character resembling that of a human.) ===
* team
Expand Down
48 changes: 43 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. Window binding. When "this" is in the global scope it references the Window or Global Object.
* 2. Implicit binding. When the function is invoked, the obj name, i.e., left of the dot is the reference for "this".
* 3. Explicit binding Using the methods call, apply (for arrays) or bind, the "this" reference can be specified.
* 4. new keyword The new keyword creates an instance of a constructor and adds a "this" object to the constructor.
*
* write out a code example of each explanation above
*/
Expand All @@ -13,14 +13,52 @@

// code example for Window Binding

function Person(name){
this.name = name;
}
console.log(this);

// Principle 2

// code example for Implicit Binding

const sythLord = {
firstName: 'Darth',
lastName: 'Vader',
fullName: function() {
return `${firstName} + " " + ${lastName}`
}
}

console.log(sythLord.fullName);



// Principle 3

// code example for New Binding

function Person(name, age){
this.name = name;
this.age = age;
}

const darthvader = new Person('darthvader', 100);
const lukeskywalker = new Person('lukeskywalker', 18);

console.log(darthvader.age);

// Principle 4

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

const spoiler = function() {
console.log("Luke, I am your " + this.relation);
};

const vader = {
relation: "father",
survival: "no"
};

spoiler.call(vader);