Skip to content

Various small fixes on the JavaScript 101 The "this" Keyword page. #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions page/javascript-101/this-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ attribution:

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:

- 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).
- 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.
- 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).
- 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.
- If the function is being invoked as a method of an object, `this` will refer to that object.
- Otherwise, the function is being invoked as a standalone function not attached to any object, and `this` will refer to the global object.

```
// A function invoked using Function.call
// A function invoked using Function.call()

var myObject = {
sayHello: function() {
console.log( "Hi! My name is " + this.myName );
Expand All @@ -31,7 +32,8 @@ myObject.sayHello.call( secondObject ); // "Hi! My name is Colin"
```

```
// A function created using Function.bind
// A function created using Function.bind()

var myName = "the global object";
var sayHello = function() {
console.log( "Hi! My name is " + this.myName );
Expand All @@ -41,12 +43,13 @@ var myObject = {
};
var myObjectHello = sayHello.bind( myObject );

sayHello(); // "Hi! My name is the global object"
sayHello(); // "Hi! My name is the global object"
myObjectHello(); // "Hi! My name is Rebecca"
```

```
// A function being attached to an object at runtime.

var myName = "the global object";
var sayHello = function() {
console.log( "Hi! My name is " + this.myName );
Expand Down