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 @@ -7,7 +7,7 @@

<title>JS III</title>

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

Expand Down
22 changes: 12 additions & 10 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,24 @@ function Hero(avatar) {
}

Hero.prototype = Object.create(Humanoid.prototype);
Hero.prototype.health = function () {
if (healthPoints <= 0)
return `${this.name} took damage!`;

Hero.prototype.hp = function () {
if (this.healthPoints <= 0){
return this.destroy();
}else{
return "Still alive!";
}
};

function Villain(avatar) {
this.team = avatar.team;
this.weapons = avatar.weapons;
this.language = avatar.language;
Humanoid.call(this, avatar);
Hero.call(this, avatar);
}

Villain.prototype = Object.create(Humanoid.prototype);
Villain.prototype.health = function () {
if (healthPoints <= 0)
return `${this.name} took damage!`;
};
Villain.prototype = Object.create(Hero.prototype);



/*
Expand Down Expand Up @@ -197,7 +198,8 @@ console.log(archer.language); // Elvish
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.

console.log(good.hp());
console.log(bad.hp())

// Stretch task:
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
Expand Down
15 changes: 15 additions & 0 deletions assignments/this.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@ console.log(person.eat());

// code example for New Binding

function Animal(food) {
this.food = food;
this.eat = function() {
console.log(`This animal likes to eat ${this.food}`);
}
}

const monkey = new Animal('banana');
const parrot = new Animal('cracker');


monkey.eat();
parrot.eat();


// Principle 4

// code example for Explicit Binding

monkey.eat.call(parrot);
parrot.eat.call(monkey);