This references the object executing the function.
- This on an object's
method
refers to theobject itself
- This on a constructor function's method refers to the
constructor function
- This on a
function
refers to theglobal scope
- This on a
function
in a method as callback function refers to theglobal scope
Note: In relation to 3
and 4
, if the container function uses strict mode
, the value of this will be undefined
instead of the global object
.
On an object's method
const person = {
name: 'Peter',
logThisContext() {
console.log(this);
}
}
person.logThisContext();
// {
// name: 'Peter',
// logThisContext: ƒ logThisContext()
// }
On a constructor function method
function Person() {
this.name = 'Peter';
this.logThisContext = function() {
console.log(this);
}
}
const peter = new Person('Peter');
peter.logThisContext();
// Person {
// name: 'Peter',
// logThisContext: ƒ (),
// __proto__: { constructor: ƒ Person() }
// }
On a function
function logContextOfThis() {
console.log(this);
}
logContextOfThis(); // Window object
On a function WITH strict mode
function logContextOfThis() {
'use strict'
console.log(this);
}
logContextOfThis(); // undefined
On a function in a method as callback
const person = {
name: 'Peter',
nicknames: ['Pet', 'P', 'Pe'],
logNicknames() {
this.nicknames.map(function() { console.log(this) });
}
}
person.logNicknames(); // Window object
On a function in a method as callback WITH strict mode
const person = {
name: 'Peter',
nicknames: ['Pet', 'P', 'Pe'],
logNicknames() {
'use strict'
this.nicknames.map(function() { console.log(this) });
}
}
person.logNicknames(); // undefined
- Arrow function
- Assign this to a variable
- Using
Function.prototype.call()
orFunction.prototype.apply()
orFunction.prototype.bind()
We can use an arrow function
which will inherit this from the container function, in this case logNicknames()
const person = {
name: 'Peter',
nicknames: ['Pet', 'P', 'Pe'],
logNicknames() {
this.nicknames.map(() => console.log(this));
}
}
person.logNicknames();
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
We can assign this to a variable
const person = {
name: 'Peter',
nicknames: ['Pet', 'P', 'Pe'],
logNicknames() {
const self = this;
this.nicknames.map(function() { console.log(self) });
}
}
person.logNicknames();
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
We can use function() {}.bind(this);
const person = {
name: 'Peter',
nicknames: ['Pet', 'P', 'Pe'],
logNicknames() {
this.nicknames
.map(function() {
console.log(this);
}.bind(this)
)
}
}
person.logNicknames();
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }
// {
// name: 'Peter',
// nicknames: [ 'Pet', 'P', 'Pe' ],
// logNicknames: ƒ logNicknames()
// }