Easy to understand 5 rules, that cover the behaviour of the "this" keyword in different contexts and helps you to master this keyword for any javascript interview.
Anuj Sharma
Last Updated Aug 29, 2024
"This" keyword is a very important concept to understand for any JavaScript Interview. 'this' keyword behaves differently in different contexts, so it is important to understand those contexts & how to know "this" value in each context.
Here are 5 examples that cover all different behaviour of the "this" keyword in JavaScript:
In the global context (outside of any function), this refers to the global object, which is the window object in a browser environment.
console.log(this === window); // true
Inside a function, the value of this depends on how the function is called. If the function is called as a method of an object, this refers to the object.
let obj = {
name: 'Alice',
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
// Case 1: How function called - with object context
obj.greet(); // "Hello, my name is Alice"
// Case 2: How function called - with window context, like window.globalFunc
const globalFunc = obj.greet;
// globalFunc invokes greet() in global context and "this" maps to windows,
// but there is no name in global object so return undefined.
globalFunc(); // "Hello, my name is"
When a function is used as a constructor (i.e., called with the new keyword), this refers to the newly created object.
function Person(name) {
this.name = name;
}
let alice = new Person('Alice');
console.log(alice.name); // "Alice"
The value of this can be explicitly set using the call, apply, or bind methods.
function greet() {
console.log(`Hello, my name is ${this.name}.`);
}
let alice = {name: 'Alice'};
greet.call(alice); // "Hello, my name is Alice."
Arrow functions do not have their value. Instead, they inherit this from the enclosing lexical scope means parent lexical scope.
let obj = {
name: 'Alice',
greet: () => {
console.log('Hello, my name is', this.name);
}
};
obj.greet(); // "Hello, my name is undefined."
Anuj Sharma
Last Updated Aug 29, 2024
Understand important web authorization techniques to enhance role-based authentication for any web application with popular techniques like Session & JSON Web Token (JWT)
Anuj Sharma
Last Updated Nov 16, 2024
A brief explanation of Cross-Origin Resource Sharing (CORS) concept to enable client application accessing resources from cross domain and HTTP headers involved to enable resource access.
Vivek Chavan
Last Updated Sep 15, 2024
You will get a clear understanding about working with any rest api and common concepts asked during interviews
Ram V
Last Updated Sep 20, 2024
Let's dive into how the function's bind method works to bind the context to any function and understand its internal workings by exploring bind method polyfill.
Anuj Sharma
Last Updated Sep 3, 2024
Most comprehensive frontend system design cheat sheet to help in approaching the system design interview in the best-structured way. Covered 7 most important frontend system design interview topics.
Anuj Sharma
Last Updated Aug 31, 2024
Understanding promise's methods is important to call APIs in parallel and it's an important concept to know for any machine coding interview.
© 2024 FrontendGeek. All rights reserved