Skip to content
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

Wade Coplen #747

Open
wants to merge 2 commits 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
64 changes: 64 additions & 0 deletions assignments/Constructor Functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Interitance in Constructors
Step One - create the Object based on key: value pairs needed for the project.
Example-
const Fred = ({
age: 45,
name: "Fred",
location: "Bedrock",
phrase: "Yabba dabba do"
});

Step Two - create the constructor called Parent.
Example -
function Parent(attributes) {
this.newAge = attributes.age;
this.newName = attributes.name;
this.newLocation = attributes.location;
this.newPhrase= attributes.phrase;
console.log(this);
}
Step Three - if we want to add a a key: value to the Object or Parent, we have to add it to the _proto_ using:
Example -
Parent.prototype.speak = function() {
return `${this.newName} says ${this.newPhrase}`;
}

Step Four - Use explicit binding to connect or transfer this:
function Parent(attributes) {
this.newAge = attributes.age;
this.newName = attributes.name;
this.newLocation = attributes.location;
this.newPhrase= attributes.phrase;
console.log(this);
}

...to this. Using the Parent.call method, allows us bind these together to create DRY coding, as to prevent us from
needing to retype the Object again.

function Child(childAttributes) {

console.log(childAttributes);
this.newToy = childAttributes.toy;
};






-----------------
const mage = new Humanoid({
createdAt: new Date(),
dimensions: {
length: 2,
width: 1,
height: 1,
},
healthPoints: 5,
name: 'Bruce',
team: 'Mage Guild',
weapons: [
'Staff of Shamalama',
],
language: 'Common Tongue',
});
4 changes: 2 additions & 2 deletions assignments/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<script src="prototypes.js"></script>
</head>

<body>

<h1>JS III - Check your work in the console!</h1>
</body>
</html>
</html>
40 changes: 33 additions & 7 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,33 @@
* name
* 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(objects) {
this.createdAt = objects.createdAt;
this.name = objects.name;
this.dimensions = objects.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(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.) ===
Expand All @@ -32,6 +51,16 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/
function Humanoid(attributes) {
CharacterStats.call(this, attributes);
this.team = attributes.team;
this.weapons = attributes.weapons;
this.language = attributes.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 +70,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 All @@ -68,10 +97,7 @@
healthPoints: 15,
name: 'Sir Mustachio',
team: 'The Round Table',
weapons: [
'Giant Sword',
'Shield',
],
weapons: ['Giant Sword', 'Shield',],
language: 'Common Tongue',
});

Expand Down Expand Up @@ -102,7 +128,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