From d963c204ca50cdf118bb495c7d11e44e612c511b Mon Sep 17 00:00:00 2001 From: Ashish-CodeJourney Date: Mon, 24 Jun 2024 18:59:40 +0530 Subject: [PATCH] Fix explanation of the 'this' keyword in JavaScript Updated the section explaining the 'this' keyword to provide a more accurate and comprehensive description of its behavior. Ensured that the explanation correctly states that 'this' refers to the object on which the method was called, addressing the issue of potential confusion for beginners. --- files/en-us/learn/javascript/objects/basics/index.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/files/en-us/learn/javascript/objects/basics/index.md b/files/en-us/learn/javascript/objects/basics/index.md index 33e70593f83e1f8..69edaaa31d0f5d5 100644 --- a/files/en-us/learn/javascript/objects/basics/index.md +++ b/files/en-us/learn/javascript/objects/basics/index.md @@ -280,9 +280,7 @@ introduceSelf() { } ``` -You are probably wondering what "this" is. The `this` keyword refers to the current object the code is being written inside — so in this case `this` is equivalent to `person`. So why not just write `person` instead? - -Well, when you only have to create a single object literal, it's not so useful. But if you create more than one, `this` enables you to use the same method definition for every object you create. +You are probably wondering what "this" is. The `this` keyword typically refers to the current object the code is being executed in. In the context of an object method, `this` refers to the object that the method was called on. Let's illustrate what we mean with a simplified pair of person objects: @@ -302,7 +300,10 @@ const person2 = { }; ``` -In this case, `person1.introduceSelf()` outputs "Hi! I'm Chris."; `person2.introduceSelf()` on the other hand outputs "Hi! I'm Deepti.", even though the method's code is exactly the same in each case. This isn't hugely useful when you are writing out object literals by hand, but it will be essential when we start using **constructors** to create more than one object from a single object definition, and that's the subject of the next section. +In this case, `person1.introduceSelf()` outputs "Hi! I'm Chris."; `person2.introduceSelf()` outputs "Hi! I'm Deepti." This happens because when the method is called, `this` refers to the object on which the method is called, which allows the same method definition to work for multiple objects. + +This isn't hugely useful when you are writing out object literals by hand, but it will be essential when we start using **constructors** to create more than one object from a single object definition, and that's the subject of the next section. +``` ## Introducing constructors