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

Skip to content

Commit d7431e5

Browse files
committed
this.md typos and style, extended setTimeout example
1 parent 660fe2d commit d7431e5

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

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)