-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrowFunctionBinding2.js
More file actions
33 lines (23 loc) · 1.04 KB
/
arrowFunctionBinding2.js
File metadata and controls
33 lines (23 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
This code defines a function `parent` that creates an object `foo` with:
- a property `bar` set to 42
- a method `qux` which is an arrow function that logs `this.bar`
Important points about arrow functions and `this`:
- Arrow functions do NOT have their own `this`.
- Instead, they capture `this` from the surrounding (lexical) scope where they are defined.
In this example:
1. `qux` is defined inside `parent`, so its `this` is the same as `this` in `parent`.
2. `parent` is called as a plain function (`parent()`), so in strict mode `this` is `undefined` inside `parent` (and thus also inside `qux`).
3. When `console.log(this.bar)` runs, `this` is `undefined`, so accessing `this.bar` results in `undefined`.
This is an example of "arrow function binding", where `this` is inherited from the enclosing scope, not from the object `foo`.
*/
function parent() {
let foo = {
bar: 42,
qux: () => {
console.log(this.bar);
}
}
foo.qux(); // Output: undefined
}
parent();