If you have any query feel free to chat us!
Happy Coding! Happy Learning!
The this
keyword is a special keyword in JavaScript which refers to the object that is currently executing the code. The value of this
is determined by the context in which it is used.
In a global context, this
refers to the global object, which is window
in a browser environment and global
in a Node.js environment.
In the context of a function, the value of this
depends on how the function is called. If a function is called as a method of an object, this
refers to the object on which the method is called. If a function is called as a standalone function, this
refers to the global object.
Arrow functions, on the other hand, inherit the this
value from the surrounding context. This means that if an arrow function is used inside a method of an object, the this
value inside the arrow function will still refer to the object.
Here is an example to illustrate the use of this
keyword:
javascriptCopy code
let person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
console.log(this.firstName + " " + this.lastName);
}
};
person.fullName(); // Outputs "John Doe"
In this example, this
refers to the person
object inside the fullName
method. When we call person.fullName()
, it will output "John Doe" because this.firstName
refers to "John" and this.lastName
refers to "Doe".
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.