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 README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Sascha Majewsky
# JavaScript - III

This challenge focuses on using the `this` keyword as well as getting comfortable with prototypes by building out a fantasy themed video game.
Expand Down
114 changes: 110 additions & 4 deletions assignments/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,31 @@
* 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(gameObjectAttributes) {
this.createdAt = gameObjectAttributes.createdAt;
this.name = gameObjectAttributes.name;
this.dimensions = gameObjectAttributes.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(characterStatsAttributes) {
GameObject.call(this, characterStatsAttributes);
this.healthPoints = characterStatsAttributes.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 +50,18 @@
* should inherit destroy() from GameObject through CharacterStats
* should inherit takeDamage() from CharacterStats
*/

function Humanoid(humanoidAttributes) {
CharacterStats.call(this, humanoidAttributes)
this.team = humanoidAttributes.team;
this.weapons = humanoidAttributes.weapons;
this.language = humanoidAttributes.language
GameObject.call(this, humanoidAttributes)
}
Humanoid.prototype = Object.create(CharacterStats.prototype)
Humanoid.prototype.greet = function () {
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 +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 Down Expand Up @@ -102,9 +131,86 @@
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.
// * Give the Hero and Villains different methods that could be used to remove health points from objects which could result in destruction if health gets to 0 or drops below 0;
// * Create two new objects, one a villain and one a hero and fight it out with methods!
// * Create two new objects, one a villain and one a hero and fight it out with methods!



/* function Player(name) {
this.name = name
}

Person.prototype.sayName = function sayName() {
console.log(`My name is ${this.name} I am team ${this.team}`);

function GreenTeamPlayer(name) {
Player.call(this, name)
this.team = 'green'
}

GreenTeamPlayer.prototype = Object.create(Player.prototype)

function RedTeamPlayer(name) {
Player.call(this, name)
this.team = 'red'
}

RedTeamPlayer.prototype = Object.create(Player.prototype)

const dan = new GreenTeamPlayer('Dan')
const cat = new RedTeamPlayer('Rosie')

dan.name()
cat.sayName()
*/

/*
# Prototypes Challenge

Overview: Study the console.log() and object method invocations at the bottom of the page. Update the Animal and Dog constructors so that the logs and methods match the commented result next to them.

Challenge: Add the missing `speak` method, complete the `eat` method
*/

/*
function Animal(attributes) {
this.weight = attributes.weight;
this.height = attributes.height;
this.food = attributes.food;
}

Animal.prototype.eat = function() {
console.log(`The ${this.animalCommonName} eats ${this.food}`);
}

Animal.prototype.speak = function() {
console.log(`${this.name} says: ${this.bark}`);
}

function Dog(dogAttributes) {
// Connect the attributes so we can use the this keyword
Animal.call(this, dogAttributes);
this.name = dogAttributes.name;
this.bark = dogAttributes.bark;
this.animalCommonName = dogAttributes.animalCommonName;
}
// Set up our __proto__ inheritance to Animal
Dog.prototype = Object.create(Animal.prototype);

const dog = new Dog({
'name': 'Dr. Doggo',
'animalCommonName': "dog",
'weight': 40,
'height': 12,
'food': 'meat',
'bark': 'Woof!'
});

console.log(dog.animalCommonName); // "dog"
dog.eat(); // "The dog eats meat"
dog.speak(); // "Dr. Doggo says: Woof!"
*/
146 changes: 141 additions & 5 deletions assignments/this.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,162 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/GLobal Binding -
* 2. Implicit Binding
* 3. New Binding -
* 4. Explicit Binding -
*
* write out a code example of each explanation above
*/

// 1) Window/GLobal Binding -
// When "this" used in a global/window scope it will bind to global/window depending on your IDE.
//
// 2) Implicit Binding -
// When you call a function and . and it gets invoked look to the left of the function and that is where "this" will bind to.
//
// 3) New Binding -
// When using the new keyword to invoke an object "this" will bind to the instance of the object.
//
// 4) Explicit Binding -
// When using explicit binding you will tell "this" how it should behave with the functions call. .bind or .apply.

// Principle 1

// code example for Window Binding
function sayName(name) {
console.log(this);
return name;
}

sayName('Sascha');

// Principle 2

// code example for Implicit Binding
const animal = {
noise: 'bark',
age: 5,
sayHello: function (name) {
console.log(`${this.noise}, I am ${name}!`);
console.log(this);
}
};

//the function got invoked to this refers to the thing left from the . notation which is here the animal object
animal.sayHello('Doggo');


// Principle 3

// code example for New Binding

let Icecream = function(color, taste, name){
// this = {}
this.color = color;
this.taste = taste;
this.name = name;
}

// When an object is invoked with the new Keyword the this will be bound to the instance of the object.
let raspberryIceCream = new Icecream("red", "raspberry", "sour");


/* other version */
/* function Person(greeter) {
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + " " + this.greeter);
console.log(this);
};
}

const jerry = new Person('jerry');
const sascha = new Person('sascha');

jerry.speak();
sascha.speak(); */


// Principle 4

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

let changeColor = function(state1, state2, state3){
console.log('My color is ' + this.color + ' and I am currently ' + state1 + " and want to " + state2 + " so I will end up as " + state3 + ".");
};

let cube = {
name: 'icecube',
color: 'blue'
};

let state = ["solid", "melt", "water"]

// .call, .apply and .bind will tell explictly how the this should behave
// .call and .apply work kinda the same by immediately invoking the function
// .bind ist like .call but returns a new function

// .call have the arguments inserted one by one
changeColor.call(cube, state[0], state[1], state[2]);
// .apply uses an array for inserting arguments
changeColor.apply(cube, state);
// this also works with the spread operator to insert the whole array as an argument
changeColor.call(cube, ...state);
// .bin will return a new function instead of invoking the original function. It will bind this to the cube object, insert the arguments and return the newFunction.
let newFunction = changeColor.bind(cube, state[0], state[1], state[2]);
console.log(newFunction());


/* other versions */

/* function Person(greeter) {
this.greeting = 'Hello';
this.greeter = greeter;
this.speak = function() {
console.log(this.greeting + " " + this.greeter);
console.log(this);
};
}


const kevin = new Person('jerry ');
const elan = new Person('sascha');

jerry.speak.call(sascha);
sascha.speak.apply(jerry);

sascha.speak();
jerry.speak();



function hello() {
console.log(this)
} */

/*
function sayHello(name) {
console.log('this', this);
console.log(`${this.greeting} my name is ${name}`);
console.log(this);
}

const myObj = {
'greeting': 'Hello',
'sayHello': sayHello
};
const travelingMe = {
greeting: 'Hola',
};

const testHello = myObj.sayHello

sayHello.call(travelingMe, 'Rosie')
sayHello.call({
greeting: 'Meow'}, 'Rosie')

const boundHello = sayHello.bind({greeting: 'Mau'}, 'Kittie')

boundHello() */