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
Copy file name to clipboardExpand all lines: fundamentals/this.md
+51-12Lines changed: 51 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,8 @@
1
1
# What is 'this'?
2
2
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.
4
4
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 (`{}`).
8
6
9
7
## Regular functions and `this`
10
8
@@ -20,7 +18,7 @@ function whatIsThis(arg) {
20
18
whatIsThis('Hello'); // --> Hello undefined
21
19
```
22
20
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).
24
22
25
23
In the example below you can see the effect (the shown output is for the browser).
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.
39
37
40
38
More info on MDN: [Strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
41
39
42
40
## Function invocation through the `call` method
43
41
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`.
@@ -70,9 +68,9 @@ More info on `this` and JavaScript function properties and methods:
70
68
71
69
## JavaScript objects and 'this'
72
70
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.
74
72
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.
76
74
77
75
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.
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).
93
91
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).
95
93
96
94
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.
97
95
@@ -136,6 +134,47 @@ class MyClass {
136
134
}
137
135
}
138
136
137
+
constmyClass=newMyClass();
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
+
classMyClass {
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
+
constmyClass=newMyClass();
158
+
myClass.sayDelayed();
159
+
```
160
+
161
+
Another way is to introduce an intermediate variable, often named `self` or `that`, in a closure:
0 commit comments