Skip to content

Commit 169f90a

Browse files
Markus Amalthea Magnusonajpiano
Markus Amalthea Magnuson
authored andcommitted
Various small fixes on the JavaScript 101 'this' Keyword page. Fixes jquery#354.
1 parent 2f65dfd commit 169f90a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

page/javascript-101/this-keyword.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ attribution:
88

99
In JavaScript, as in most object-oriented programming languages, `this` is a special keyword that is used in methods to refer to the object on which a method is being invoked. The value of `this` is determined using a simple series of steps:
1010

11-
- If the function is invoked using `Function.call` or `Function.apply`, this will be set to the first argument passed to `.call()`/`.apply()`. If the first argument passed to `.call()`/`.apply()` is `null` or `undefined`, `this` will refer to the global object (which is the `window` object in web browsers).
12-
- If the function being invoked was created using `Function.bind`, `this` will be the first argument that was passed to `.bind()` at the time the function was created.
11+
- If the function is invoked using `Function.call()` or `Function.apply()`, this will be set to the first argument passed to `.call()`/`.apply()`. If the first argument passed to `.call()`/`.apply()` is `null` or `undefined`, `this` will refer to the global object (which is the `window` object in web browsers).
12+
- If the function being invoked was created using `Function.bind()`, `this` will be the first argument that was passed to `.bind()` at the time the function was created.
1313
- If the function is being invoked as a method of an object, `this` will refer to that object.
1414
- Otherwise, the function is being invoked as a standalone function not attached to any object, and `this` will refer to the global object.
1515

1616
```
17-
// A function invoked using Function.call
17+
// A function invoked using Function.call()
18+
1819
var myObject = {
1920
sayHello: function() {
2021
console.log( "Hi! My name is " + this.myName );
@@ -31,7 +32,8 @@ myObject.sayHello.call( secondObject ); // "Hi! My name is Colin"
3132
```
3233

3334
```
34-
// A function created using Function.bind
35+
// A function created using Function.bind()
36+
3537
var myName = "the global object";
3638
var sayHello = function() {
3739
console.log( "Hi! My name is " + this.myName );
@@ -41,12 +43,13 @@ var myObject = {
4143
};
4244
var myObjectHello = sayHello.bind( myObject );
4345
44-
sayHello(); // "Hi! My name is the global object"
46+
sayHello(); // "Hi! My name is the global object"
4547
myObjectHello(); // "Hi! My name is Rebecca"
4648
```
4749

4850
```
4951
// A function being attached to an object at runtime.
52+
5053
var myName = "the global object";
5154
var sayHello = function() {
5255
console.log( "Hi! My name is " + this.myName );

0 commit comments

Comments
 (0)