Appearance
this
thisat the global level refers to the global object (window/global).thisis determined by who calls the function, not where it lives.- In a browser, the global object is
window. - In Node.js, the global object is
global.
Strict and Non-Strict Mode
- The value of
thisinside a function depends on strict or non-strict mode when it isundefinedornull.- In strict mode, the value of
thisremainsundefined. - In non-strict mode, the value of
thisiswindow.
- In strict mode, the value of
- This happens due to
thissubstitution. - If the value of the
thiskeyword isundefinedornull, thethiskeyword is replaced with the global object only in non-strict mode. - The value of the
thiskeyword depends on how the function is called.
js
"use strict"
function x() {
console.log(this)
}
x(); // undefined
window.x() // window objectjs
const obj = {
a: 10,
x: function() {
console.log(this);
}
}
obj.x(); // objcall()
- Calls immediately.
js
const student1 = {
name: "Chandan",
printName: function () {
console.log(this.name);
},
};
const student2 = {
name: "Harsh",
};
student1.printName.call(student2); // Harsh (function borrowing)js
let name = {
firstName: "Chandan",
lastName: "Sahoo",
};
let printFullName = function (hometown, state) {
console.log(
this.firstName + " " + this.lastName + " from " + hometown + " , " + state,
);
};
printFullName.call(name, "Kendrapada", "Odisha");
let name2 = {
firstName: "Harsh",
lastName: "Bansal",
};
printFullName.call(name2, "Delhi", "Delhi");apply()
- Calls immediately.
js
printFullName.apply(name1, ["Kendrapada", "Odisha"]);bind()
- Returns a new function.
js
let printMyName = printFullName.bind(name1, "Kendrapada", "Odisha");
console.log(printMyName);
printMyName();js
const user = {
name: "Chandan",
sayHi() {
console.log(this.name);
},
};
setTimeout(user.sayHi, 1000); // this will fail
// This is a reference to the function, but it is detached from the actual object.
// const fn = user.sayHi
// setTimeout(fn, 1000);
setTimeout(user.sayHi.bind(user), 1000); // this will passArrow Functions
- An arrow function does not have its own
thisbinding; it retains the value ofthisfrom its enclosing lexical context.
js
const obj = {
name: "Chandan",
regular: function() {
console.log(this.name) // "Chandan"
},
arrow: () => {
console.log(this.name) // undefined — this is window
}
}Special Example
js
const obj = {
name: "Chandan",
outer: function() {
console.log(this.name) // "Chandan"
function inner() {
console.log(this.name) // undefined (strict) or window (non-strict)
}
inner()
}
}Fixed using an arrow function:
js
outer: function() {
const inner = () => {
console.log(this.name) // inherits this from outer
}
inner()
}