Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
codepen-constructor.js
68 changes: 68 additions & 0 deletions assignments/codepen-constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function Fruit(attrs) {
this.type = attrs.type;
this.name = attrs.name;
this.isRipe = attrs.isRipe;
this.calories = attrs.calories;
}

Fruit.prototype.shipped = function(destination) {
console.log(`Shipping ${this.name} to ${destination}`);
};

Fruit.prototype.calculateCals = function() {
console.log(`Calories in ${this.name} are ${this.calories * 200}`);
};

function Banana(bananaAttrs) {
Fruit.call(this, bananaAttrs);
this.doMonkeysLikeIt = bananaAttrs.doMonkeysLikeIt;
}

Banana.prototype = Object.create(Fruit.prototype);

Banana.prototype.checkIfMonkeysLikeIt = function() {
if(this.doMonkeysLikeIt) {
return true;
} else {
return false;
}
};

function Kiwi(kiwiAttrs) {
Fruit.call(this, kiwiAttrs);
this.isFuzzy = kiwiAttrs.isFuzzy;
}

Kiwi.prototype = Object.create(Fruit.prototype);

Kiwi.prototype.checkIfFuzzy = function() {
if(this.isFuzzy) {
return true;
} else {
return false;
}
}

const newBanana = new Banana({
doMonkeysLikeIt: true,
type: 'Tree',
name: 'Banana',
isRipe: false,
calories: 0.1
});

const newKiwi = new Kiwi({
isFuzzy: true,
type: 'Tree',
name: 'Kiwi',
isRipe: false,
calories: 0.7
});

console.log(newBanana.shipped('Alaska'));
console.log(newKiwi.shipped('Colorado'));
console.log(newBanana.checkIfMonkeysLikeIt());
console.log(newKiwi.checkIfFuzzy());
console.log(newBanana.calculateCals());
console.log(newKiwi.calculateCals());

2 changes: 1 addition & 1 deletion assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<title>JS III</title>

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

<body>
Expand Down
35 changes: 33 additions & 2 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,32 @@
* 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(arguments) {
this.createdAt = arguments.createdAt;
this.name = arguments.name;
this.dimensions = arguments.dimensions;
}
GameObject.prototype.destroy = function(name) {
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(arguments) {
GameObject.call(this,arguments);
this.healthPoints = arguments.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 +51,19 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(arguments) {
CharacterStats.call(this,arguments);
this.team = arguments.team;
this.weapons = arguments.weapons;
this.language = arguments.language;
}

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

Humanoid.prototype.greet = function(name,language) {
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.
Expand All @@ -41,7 +72,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 +133,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
51 changes: 45 additions & 6 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
/* 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 - objects are available at the highest level, Window/Global, and can be accessed by lower levels
* 2. Implicit Binding - objects bind to their own properties and methods
* 3. New Binding - object instances bind to properties/medthods of their prototype objects
* 4. Explicit Binding - object instances bind to their inherted object instance
*
* write out a code example of each explanation above
*/

// Principle 1

// code example for Window Binding
function windowPop(name) {
this.name=name;
// window.alert(name);
console.log(name);
return name;
}
windowPop("bryant");

// Principle 2

// code example for Implicit Binding

const hello = {
greeting: "Hi, you must be",
sayHi: function(name) {
console.log( `${this.greeting} ${name}`);
return name;
}
}
hello.sayHi('bryant');

// Principle 3

// code example for New Binding
function Visitor(name) {
this.greeting = "Welcome fellow human ";
this.name = name;
this.sayHi = function() {
console.log( `${this.greeting}, you must be ${name}`);
}
}
let bp = new Visitor('bryant');

bp.sayHi();

// // Principle 4

// // code example for Explicit Binding

function Bot (name) {
Visitor.call(this,name);
this.greeting = "Beep beep bit fellow bot"

}
Bot.prototype = Object.create(Visitor.prototype);
let bot = new Bot('spam bot');
bot.sayHi();
bot.sayHi.apply(bp);


// Principle 4

// code example for Explicit Binding