In this tutorial, we'll be discussing the concept of this in JavaScript. We'll look at what this is and how it works in JavaScript. We'll also look at how to use it in various situations, including functions, classes, and arrow functions.
console.log(this);
In this example, we are using the 'this' keyword implicitly to refer to the global object.
const obj = {
name: 'John',
sayName: function() {
console.log(this);
}
};
obj.sayName();
In this example, we are using the 'this' keyword explicitly to refer to the object that is passed as an argument.
function sayName() {
console.log(this);
}
sayName();
In this example, we are using the 'this' keyword to refer to the function itself.
const obj = {
name: 'John',
sayName: function() {
console.log(this);
}
};
obj.sayName();
In this example, we are using the 'this' keyword to refer to the object itself.