Skip to content

Commit ca55d08

Browse files
authored
Merge pull request #1 from SaschaMajewsky/sascha-majewsky
changed the readme.md to own my name
2 parents 33d1564 + 933be7f commit ca55d08

3 files changed

Lines changed: 252 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Sascha Majewsky
12
# JavaScript - III
23

34
This challenge focuses on using the `this` keyword as well as getting comfortable with prototypes by building out a fantasy themed video game.

‎assignments/prototypes.js‎

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,31 @@
1515
* dimensions (These represent the character's size in the video game)
1616
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
1717
*/
18+
function GameObject(gameObjectAttributes) {
19+
this.createdAt = gameObjectAttributes.createdAt;
20+
this.name = gameObjectAttributes.name;
21+
this.dimensions = gameObjectAttributes.dimensions;
22+
}
23+
24+
GameObject.prototype.destroy = function () {
25+
return `${this.name} was removed from the game.`;
26+
};
1827

1928
/*
2029
=== CharacterStats ===
2130
* healthPoints
2231
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
2332
* should inherit destroy() from GameObject's prototype
2433
*/
34+
function CharacterStats(characterStatsAttributes) {
35+
GameObject.call(this, characterStatsAttributes);
36+
this.healthPoints = characterStatsAttributes.healthPoints;
37+
}
38+
39+
CharacterStats.prototype = Object.create(GameObject.prototype)
40+
CharacterStats.prototype.takeDamage = function() {
41+
return `${this.name} took damage`;
42+
}
2543

2644
/*
2745
=== Humanoid (Having an appearance or character resembling that of a human.) ===
@@ -32,7 +50,18 @@
3250
* should inherit destroy() from GameObject through CharacterStats
3351
* should inherit takeDamage() from CharacterStats
3452
*/
35-
53+
function Humanoid(humanoidAttributes) {
54+
CharacterStats.call(this, humanoidAttributes)
55+
this.team = humanoidAttributes.team;
56+
this.weapons = humanoidAttributes.weapons;
57+
this.language = humanoidAttributes.language
58+
GameObject.call(this, humanoidAttributes)
59+
}
60+
Humanoid.prototype = Object.create(CharacterStats.prototype)
61+
Humanoid.prototype.greet = function () {
62+
return `${this.name} offers a greeting in ${this.language}.`
63+
};
64+
3665
/*
3766
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
3867
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
@@ -41,7 +70,7 @@
4170

4271
// Test you work by un-commenting these 3 objects and the list of console logs below:
4372

44-
/*
73+
4574
const mage = new Humanoid({
4675
createdAt: new Date(),
4776
dimensions: {
@@ -102,9 +131,86 @@
102131
console.log(archer.greet()); // Lilith offers a greeting in Elvish.
103132
console.log(mage.takeDamage()); // Bruce took damage.
104133
console.log(swordsman.destroy()); // Sir Mustachio was removed from the game.
105-
*/
134+
106135

107136
// Stretch task:
108137
// * Create Villain and Hero constructor functions that inherit from the Humanoid constructor function.
109138
// * 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;
110-
// * Create two new objects, one a villain and one a hero and fight it out with methods!
139+
// * Create two new objects, one a villain and one a hero and fight it out with methods!
140+
141+
142+
143+
/* function Player(name) {
144+
this.name = name
145+
}
146+
147+
Person.prototype.sayName = function sayName() {
148+
console.log(`My name is ${this.name} I am team ${this.team}`);
149+
150+
function GreenTeamPlayer(name) {
151+
Player.call(this, name)
152+
this.team = 'green'
153+
}
154+
155+
GreenTeamPlayer.prototype = Object.create(Player.prototype)
156+
157+
function RedTeamPlayer(name) {
158+
Player.call(this, name)
159+
this.team = 'red'
160+
}
161+
162+
RedTeamPlayer.prototype = Object.create(Player.prototype)
163+
164+
const dan = new GreenTeamPlayer('Dan')
165+
const cat = new RedTeamPlayer('Rosie')
166+
167+
dan.name()
168+
cat.sayName()
169+
*/
170+
171+
/*
172+
# Prototypes Challenge
173+
174+
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.
175+
176+
Challenge: Add the missing `speak` method, complete the `eat` method
177+
*/
178+
179+
/*
180+
function Animal(attributes) {
181+
this.weight = attributes.weight;
182+
this.height = attributes.height;
183+
this.food = attributes.food;
184+
}
185+
186+
Animal.prototype.eat = function() {
187+
console.log(`The ${this.animalCommonName} eats ${this.food}`);
188+
}
189+
190+
Animal.prototype.speak = function() {
191+
console.log(`${this.name} says: ${this.bark}`);
192+
}
193+
194+
function Dog(dogAttributes) {
195+
// Connect the attributes so we can use the this keyword
196+
Animal.call(this, dogAttributes);
197+
this.name = dogAttributes.name;
198+
this.bark = dogAttributes.bark;
199+
this.animalCommonName = dogAttributes.animalCommonName;
200+
}
201+
// Set up our __proto__ inheritance to Animal
202+
Dog.prototype = Object.create(Animal.prototype);
203+
204+
const dog = new Dog({
205+
'name': 'Dr. Doggo',
206+
'animalCommonName': "dog",
207+
'weight': 40,
208+
'height': 12,
209+
'food': 'meat',
210+
'bark': 'Woof!'
211+
});
212+
213+
console.log(dog.animalCommonName); // "dog"
214+
dog.eat(); // "The dog eats meat"
215+
dog.speak(); // "Dr. Doggo says: Woof!"
216+
*/

‎assignments/this.js‎

Lines changed: 141 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,162 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. Window/GLobal Binding -
5+
* 2. Implicit Binding
6+
* 3. New Binding -
7+
* 4. Explicit Binding -
88
*
99
* write out a code example of each explanation above
1010
*/
1111

12+
// 1) Window/GLobal Binding -
13+
// When "this" used in a global/window scope it will bind to global/window depending on your IDE.
14+
//
15+
// 2) Implicit Binding -
16+
// 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.
17+
//
18+
// 3) New Binding -
19+
// When using the new keyword to invoke an object "this" will bind to the instance of the object.
20+
//
21+
// 4) Explicit Binding -
22+
// When using explicit binding you will tell "this" how it should behave with the functions call. .bind or .apply.
23+
1224
// Principle 1
1325

1426
// code example for Window Binding
27+
function sayName(name) {
28+
console.log(this);
29+
return name;
30+
}
31+
32+
sayName('Sascha');
1533

1634
// Principle 2
1735

1836
// code example for Implicit Binding
37+
const animal = {
38+
noise: 'bark',
39+
age: 5,
40+
sayHello: function (name) {
41+
console.log(`${this.noise}, I am ${name}!`);
42+
console.log(this);
43+
}
44+
};
45+
46+
//the function got invoked to this refers to the thing left from the . notation which is here the animal object
47+
animal.sayHello('Doggo');
48+
1949

2050
// Principle 3
2151

2252
// code example for New Binding
2353

54+
let Icecream = function(color, taste, name){
55+
// this = {}
56+
this.color = color;
57+
this.taste = taste;
58+
this.name = name;
59+
}
60+
61+
// When an object is invoked with the new Keyword the this will be bound to the instance of the object.
62+
let raspberryIceCream = new Icecream("red", "raspberry", "sour");
63+
64+
65+
/* other version */
66+
/* function Person(greeter) {
67+
this.greeting = 'Hello';
68+
this.greeter = greeter;
69+
this.speak = function() {
70+
console.log(this.greeting + " " + this.greeter);
71+
console.log(this);
72+
};
73+
}
74+
75+
const jerry = new Person('jerry');
76+
const sascha = new Person('sascha');
77+
78+
jerry.speak();
79+
sascha.speak(); */
80+
81+
2482
// Principle 4
2583

26-
// code example for Explicit Binding
84+
// code example for Explicit Binding
85+
86+
let changeColor = function(state1, state2, state3){
87+
console.log('My color is ' + this.color + ' and I am currently ' + state1 + " and want to " + state2 + " so I will end up as " + state3 + ".");
88+
};
89+
90+
let cube = {
91+
name: 'icecube',
92+
color: 'blue'
93+
};
94+
95+
let state = ["solid", "melt", "water"]
96+
97+
// .call, .apply and .bind will tell explictly how the this should behave
98+
// .call and .apply work kinda the same by immediately invoking the function
99+
// .bind ist like .call but returns a new function
100+
101+
// .call have the arguments inserted one by one
102+
changeColor.call(cube, state[0], state[1], state[2]);
103+
// .apply uses an array for inserting arguments
104+
changeColor.apply(cube, state);
105+
// this also works with the spread operator to insert the whole array as an argument
106+
changeColor.call(cube, ...state);
107+
// .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.
108+
let newFunction = changeColor.bind(cube, state[0], state[1], state[2]);
109+
console.log(newFunction());
110+
111+
112+
/* other versions */
113+
114+
/* function Person(greeter) {
115+
this.greeting = 'Hello';
116+
this.greeter = greeter;
117+
this.speak = function() {
118+
console.log(this.greeting + " " + this.greeter);
119+
console.log(this);
120+
};
121+
}
122+
123+
124+
const kevin = new Person('jerry ');
125+
const elan = new Person('sascha');
126+
127+
jerry.speak.call(sascha);
128+
sascha.speak.apply(jerry);
129+
130+
sascha.speak();
131+
jerry.speak();
132+
133+
134+
135+
function hello() {
136+
console.log(this)
137+
} */
138+
139+
/*
140+
function sayHello(name) {
141+
console.log('this', this);
142+
console.log(`${this.greeting} my name is ${name}`);
143+
console.log(this);
144+
}
145+
146+
const myObj = {
147+
'greeting': 'Hello',
148+
'sayHello': sayHello
149+
};
150+
const travelingMe = {
151+
greeting: 'Hola',
152+
};
153+
154+
const testHello = myObj.sayHello
155+
156+
sayHello.call(travelingMe, 'Rosie')
157+
sayHello.call({
158+
greeting: 'Meow'}, 'Rosie')
159+
160+
const boundHello = sayHello.bind({greeting: 'Mau'}, 'Kittie')
161+
162+
boundHello() */

0 commit comments

Comments
 (0)