Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42aa079

Browse files
authored
Merge pull request HackYourFuture#34 from remarcmij/master
Updated this.md, homework week 7 Step 5
2 parents 5ff6907 + d7431e5 commit 42aa079

File tree

2 files changed

+125
-38
lines changed

2 files changed

+125
-38
lines changed

Week7/MAKEME.md

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Give feedback on Step 4 and 5 of last weeks homework. Please provide the feedbac
2222

2323
_Deadline Monday_
2424

25-
- Solve all your Git issues. DO NO CLOSE AN ISSUE WITHOUT AN EXPLANATION OR CODE COMMIT REFERENCING THAT ISSUE.
25+
- Solve all your Git issues. DO NO CLOSE AN ISSUE WITHOUT AN EXPLANATION OR CODE COMMIT REFERENCING THAT ISSUE.
2626

2727

2828
## Step 3: Fix issues
@@ -37,7 +37,7 @@ _Deadline Saturday_
3737

3838
Let's practice working with Objects and Arrays. Go to FreeCodeCamp and complete all challenges under "Object Oriented and Functional Programming" and the _first four challenges_ under "Basic Algorithm Scripting", up until 'Find the longest word in a string.'
3939

40-
Also make:
40+
Also make:
4141

4242
1. [Comparisons with the Logical And Operator](https://www.freecodecamp.com/challenges/comparisons-with-the-logical-and-operator)
4343

@@ -49,38 +49,86 @@ Also make:
4949

5050
_Deadline Wednesday_
5151

52+
Complete the following code:
53+
5254
```js
53-
// Inspired by the lesson.
54-
// Create the following objects: Movies, Staff.
55-
56-
//Fill the following methods:
57-
Movies {
58-
getStars()
59-
getWrites()
60-
getDirector()
61-
getRating()
62-
rate()
55+
class Movie {
56+
constructor(title, director) {
57+
// add your code here
58+
}
59+
60+
getTitle() {
61+
// add your code here
62+
}
63+
64+
getDirector() {
65+
// add your code here
66+
}
67+
68+
addStar(star) {
69+
// add your code here
70+
}
71+
72+
getStars() {
73+
// add your code here
74+
}
75+
76+
addWriter(writer) {
77+
// add your code here
78+
}
79+
80+
getWriters() {
81+
// add your code here
82+
}
83+
84+
addRating(rating) {
85+
// add your code here
86+
}
87+
88+
getAverageRating() {
89+
// add your code here
90+
}
91+
6392
// ... Add yours :-) Look to IMDB for inspiration
6493
}
6594

66-
Staff {
67-
getName()
68-
getRole()
69-
getAge()
70-
// ... Add yours :-) Look to IMDB for inspiration
95+
class StaffMember {
96+
constructor(name, role, dateOfBirth) {
97+
// add your code here
98+
}
99+
100+
addMovie(movie) {
101+
// add your code here
102+
}
103+
104+
getName() {
105+
// add your code here
106+
}
107+
108+
getRole() {
109+
// add your code here
110+
}
111+
112+
getAge() {
113+
// add your code here
114+
}
71115
}
72116

73-
// Initialize the objects
74-
// by pick your favorite movie from http://www.imdb.com/
75-
// and make sure that the following actions work.
76-
console.log(InstanceMovie.getStars().map(actor => `${actor.getName()} ${actor.getAge()}`));
77-
const director = InstanceMovie.getDirector();
117+
// Pick your favorite movie from http://www.imdb.com/
118+
119+
const myMovie = new Movie(...);
120+
121+
const firstActor = new StaffMember(...);
122+
myMovie.addStar(firstActor);
123+
// create and add more staff members
124+
125+
// Make sure that the following actions work.
126+
console.log(myMovie.getStars().map(actor => `${actor.getName()} ${actor.getAge()}`));
127+
const director = myMovie.getDirector();
78128
console.log(`Director: ${director.map(director => `${director.getName()}`)}`);
79-
// Be creative with this let's see what you come up with :-)
80129
```
81130

82-
Fun extra step: If you get bored, template them and make a page by rendering the results in HTML :slightly_smiling_face:
83-
with something like `document.querySelector('.move').innerHTML = MovieHTML`
131+
Fun extra step: If you get bored, template them and make a page by rendering the results in HTML :smile: with something like `document.querySelector('.move').innerHTML = ...`
84132

85133
## Step 6: Read before next lecture
86134

@@ -115,7 +163,7 @@ Remember the person with the most kata points gets a prize from Gijs (and you ca
115163
1. [Stacks/Queues](https://www.youtube.com/watch?v=wjI1WNcIntg) (5 mins)
116164
2. [JS Event Loops](https://www.youtube.com/watch?v=8aGhZQkoFbQ) (26 mins, watch this one twice or until you understand it)
117165

118-
>Create a new repository "hyf-javascript3". Also create a new folder "week1" inside this repository.
166+
>Create a new repository "hyf-javascript3". Also create a new folder "week1" inside this repository.
119167
Upload your homework files inside the week1 folder and write a description for this “commit”.
120168
Your hyf-javascript3/week1 should now contain the files of your homework.
121169
Place the link to your repository folder in Trello.

fundamentals/this.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# What is 'this'?
22

3-
At any point during the execution of a JavaScript program there is a context-dependent value that you can access through the keyword `this`. In many cases the value of `this` is `undefined` and as such not of significance for use in your own code.
3+
At any point during the execution of a JavaScript program there is a context-dependent value that you can access through the keyword `this`. Often, the value of `this` is simply `undefined` and therefore not of much significance for use in your own code.
44

5-
Note: The value of `this` is only useful if used inside a function.
6-
7-
> When accessed outside of any function, the value of `this` is different depending on whether you run your program in the browser or in Node. In the case of the browser, the value of `this` refers to the global `window` object. In the case of Node, the value of `this` outside any function is an empty object (`{}`).
5+
> Note: The value of `this` is only useful if used inside a function. When accessed outside of any function, the value of `this` is different depending on whether you run your program in the browser or in Node. In the case of the browser, the value of `this` refers to the global `window` object. In the case of Node, the value of `this` outside any function is an empty object (`{}`).
86
97
## Regular functions and `this`
108

@@ -20,7 +18,7 @@ function whatIsThis(arg) {
2018
whatIsThis('Hello'); // --> Hello undefined
2119
```
2220

23-
As mentioned, the value of `this` in this case is `undefined`. Note however that this is only the case if we start our file with the string literal `'use strict'`. In versions of JavaScript prior to ES5 the `'use strict'` option did not exist. If you leave out `'use strict'` then `this` refers to the 'global context' (in the browser this is the `window` object, in Node it is the `global` object).
21+
As mentioned, the value of `this` in this case is `undefined`. Note however that this is only the case if we start our file with the string literal `'use strict'`. (In versions of JavaScript prior to ES5 the `'use strict'` option did not exist.) If you leave out `'use strict'` then `this` refers to the 'global context' (in the browser this is the `window` object, in Node it is the `global` object).
2422

2523
In the example below you can see the effect (the shown output is for the browser).
2624

@@ -35,13 +33,13 @@ whatIsThis('Hello'); // --> Hello
3533
// ▶︎ Window {postMessage: f, ...}
3634
```
3735

38-
Accessing the global context through `this` (accidentally on or on purpose) is never a good idea, especially when it comes to forgetting to declare a variable. The designers of JavaScript recognised this as an issue and provided the `'use strict'` option in ES5 to remedy the issue.
36+
Accessing the global context through `this` (accidentally or on purpose) is never a good idea, especially when it comes to forgetting to declare a variable. The designers of JavaScript recognised this as an issue and provided the `'use strict'` option in ES5 as a remedy.
3937

4038
More info on MDN: [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
4139

4240
## Function invocation through the `call` method
4341

44-
When you call a regular function by specifying its name followed by zero or more arguments enclosed within parentheses, the JavaScript engine actually invokes the `call` method that exists on every function (yes, functions are actually a special type of JavaScript objects). The code snippet show how a regular function is invoked behind the scenes by the JavaScript engine, setting the `this` value (the first argument of the `call` method) to `undefined`.
42+
When you call a regular function by specifying its name followed by zero or more arguments enclosed within parentheses, the JavaScript engine in fact invokes the `call` method that exists on every function (yes, a function is actually a special type of JavaScript object). The code snippet below shows how a regular function is invoked behind the scenes by the JavaScript engine, setting the `this` value (the first argument of the `call` method) to `undefined`.
4543

4644
```js
4745
'use strict';
@@ -53,7 +51,7 @@ function whatIsThis(arg) {
5351
whatIsThis.call(undefined, 'Hello'); // --> Hello undefined
5452
```
5553

56-
We can use the `call` method ourselves and pass something else in place of `undefined`, as show in the next snippet:
54+
We can use the `call` method ourselves and pass something else in place of `undefined`, as shown in the next snippet:
5755

5856
```js
5957
whatIsThis.call('world!', 'Hello'); // --> Hello world!
@@ -70,9 +68,9 @@ More info on `this` and JavaScript function properties and methods:
7068

7169
## JavaScript objects and 'this'
7270

73-
When used in conjunction with JavaScript object methods (including those from ES6 classes) the `this` keyword gets overriding importance.
71+
When used in conjunction with JavaScript methods (including those from ES6 classes) the `this` keyword gets overriding importance.
7472

75-
**What is a method?** A method is a regular JavaScript function that is 'called on' an object, using dot notation. In almost all cases this function will be defined as a property of the object (or it's prototype) it is called upon. In ES6 classes, methods are directly defined as member functions on the class.
73+
> **What is a method?** A method is a regular JavaScript function that is 'called on' an object, using dot notation. In almost all cases this function will be defined as a property of the object (or it's prototype) it is called upon. In ES6 classes, methods are directly defined as member functions on the class.
7674
7775
The example below shows a simple object with a data property `myData` and a method property `myMethod`. When `myMethod` is called using dot notation such as shown in the example below, the `this` value inside the method is set to the object itself. Hence, `myMethod` has access to the `myData` property through the `this` keyword.
7876

@@ -89,9 +87,9 @@ myObj.myMethod(); // --> Hello world!
8987

9088
## Function.prototype.bind
9189

92-
There is yet another way to set the `this` value, this time fixing it's value. This can be done through the `bind` method that is avaiable on every function (although it has no effect on fat arrow functions).
90+
There is yet another way to set the `this` value. This can be done through the `bind` method that is avaiable on every function (although it has no effect on fat arrow functions).
9391

94-
To just fix the value of `this` you call the `bind` method with a single parameter, passing the value to be assigned to `this`. (The `bind` method accepts additional parameters, but their use is beyond the scope of this article. See the reference below for more information).
92+
To set the value of `this` you call the `bind` method with a single parameter, passing the value to be assigned to `this`. (The `bind` method accepts additional parameters, but their use is beyond the scope of this article. See the reference below for more information).
9593

9694
The `bind` method returns a new function for which the `this` value is fixed to the value specified in the `bind` parameter, as shown below.
9795

@@ -136,6 +134,47 @@ class MyClass {
136134
}
137135
}
138136

137+
const myClass = new MyClass();
138+
myClass.sayDelayed();
139+
```
140+
141+
Nevertheless, there are ways to use regular functions in this scenario as was the common case before fat arrow functions were introduced in ES6. One way is to use `bind` as in this example:
142+
143+
```js
144+
class MyClass {
145+
146+
constructor() {
147+
this.myData = 'Hello world'
148+
}
149+
150+
sayDelayed() {
151+
setTimeout(function () {
152+
console.log(this.myData);
153+
}.bind(this), 1000);
154+
}
155+
}
156+
157+
const myClass = new MyClass();
158+
myClass.sayDelayed();
159+
```
160+
161+
Another way is to introduce an intermediate variable, often named `self` or `that`, in a closure:
162+
163+
```js
164+
class MyClass {
165+
166+
constructor() {
167+
this.myData = 'Hello world'
168+
}
169+
170+
sayDelayed() {
171+
const self = this;
172+
setTimeout(function () {
173+
console.log(self.myData);
174+
}, 1000);
175+
}
176+
}
177+
139178
const myClass = new MyClass();
140179
myClass.sayDelayed();
141180
```

0 commit comments

Comments
 (0)