You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Arrow functions can’t be used as constructors as other functions can.
279
282
- If you attempt to use new with an arrow function, it will throw an error.
280
283
- To create class-like objects in JavaScript, you should use the new ES6 classes instead
281
-
- Arrow func exmaples
282
284
283
-
```
285
+
The this keyword works differently in arrow functions.
286
+
287
+
- The `this` value inside the arrow function gets binded and calcuated and assigned based on its wrapper/container/parent `this` value.
288
+
- The methods call(), apply(), and bind() will not change the value of this in arrow functions
289
+
### Example
290
+
```javascript
284
291
// ES5
285
292
varmultiplyES5=function (x, y) {
286
293
return x * y;
@@ -289,14 +296,8 @@ var multiplyES5 = function (x, y) {
289
296
// ES6
290
297
constmultiplyES6= (x, y) => { return x * y };
291
298
```
292
-
293
-
The this keyword works differently in arrow functions.
294
-
295
-
- The `this` value inside the arrow function gets binded and calcuated and assigned based on its wrapper/container/parent `this` value.
296
-
- The methods call(), apply(), and bind() will not change the value of this in arrow functions
297
-
298
299
#### “this” with fat arrow function
299
-
```
300
+
```javascript
300
301
functionPerson(fn, ln) {
301
302
this.first_name= fn;
302
303
this.last_name= ln;
@@ -313,7 +314,7 @@ person1.displayName(); // this doesn't equal window, because it gets `this` that
313
314
314
315
In the following example, the foo1() gets the `window` as `this` value, because on interpretation time, the interpreter assign the `this` immediately based on the surrounding execution context which is `window` in the case of simple literal object.
315
316
316
-
```
317
+
```javascript
317
318
let user = {
318
319
count:10,
319
320
foo1: () => {
@@ -324,11 +325,10 @@ let user = {
324
325
let user1 =user.foo1() // this equals window
325
326
```
326
327
327
-
## test your knowledge
328
-
329
-
In this example, let the students guess the result and then go line by line as if you were an interpreter and execute the code. Or use the debugger tools on devtools to execute line by line.
328
+
### Excercise
329
+
In this excercise, let the students guess the result and then go line by line as if you were an interpreter and execute the code. Or use the debugger tools on devtools to execute line by line.
330
330
331
-
```
331
+
```javascript
332
332
functionmultiply(p, q, callback) {
333
333
callback(p * q);
334
334
}
@@ -348,11 +348,7 @@ user.findMultiply();
348
348
//Prints 6
349
349
//Prints true
350
350
```
351
+
### Essence
352
+
351
353
352
-
## Wrapping up
353
354
354
-
### `this` rules
355
-
- By default, “this” refers to global object which is `global` in case of NodeJS and `window` object in case of browser
356
-
- When a method is called as a property of object, then `this` refers to the parent object
357
-
- When a function is called with `new` operator then `this` refers to the newly created instance.
358
-
- When a function is called using `call` and `apply` functions then `this` refers to the value passed as first argument of call or apply method.
0 commit comments