Hello fellow tech mates, am so happy to be writing this short article on the "this" keyword.
For a long time i had issues while learning javascript and mainly, i didn't grasp the concept about the "this" keyword until recently when i came across a tutorial by the thenetninjauk, he gave a good and clear understanding about it. The "this " keyword is a context object and it represents the context in which the entire code is executed. (might still sound confusing right, i'll break it down a little more).
The JavaScript this keyword refers to the object it belongs to. It has different values depending on where it is used: . In a function, this refers to the global object. In a function, in strict mode, this is undefined . w3school
let Person = {
firstName: "dumto",
lastName : "narth",
id : 1134,
fullName() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName());
//Javascript sets the this keyword to be the object the -
// method was used on
In an object method, this refers to the "owner" of the method, and who is the owner of the fullName method (it's the Person object). In the example above, this refers to the Person object.
Thank you so much if you took out your time to read this, would still be writing more articles so sit back and relax