From 3b2abbdadbe0d7e25cf69069681b34cd1d855602 Mon Sep 17 00:00:00 2001 From: Anastasios Poursaitides Date: Sat, 5 Oct 2024 01:18:28 +0300 Subject: [PATCH] Update article.md with information about the use of thisArg on functions. The this argument on functions is a very nuanced but often mistreated topic. More clarification and documentation is needed to avoid bugs. --- 1-js/05-data-types/05-array-methods/article.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/1-js/05-data-types/05-array-methods/article.md b/1-js/05-data-types/05-array-methods/article.md index 8536459582..2af4c5ac26 100644 --- a/1-js/05-data-types/05-array-methods/article.md +++ b/1-js/05-data-types/05-array-methods/article.md @@ -701,7 +701,11 @@ arr.map(func, thisArg); // thisArg is the optional last argument ``` -The value of `thisArg` parameter becomes `this` for `func`. +The value of `thisArg` parameter becomes `this` for `func`. It actualy binds a this context inside a function that uses the 'this' keyword. + +When a function is called as a method on an object, any 'this' keyword that is used inside the function points to that object, so it's not necessary to bind it explicitly. +In other contexts however, and in particular in arrow functions where they could be called from a different context from where they were originally defined, +a 'this' binding is useful as it avoids errors and confusion as to what 'this' context is being used in a function. For example, here we use a method of `army` object as a filter, and `thisArg` passes the context: @@ -731,7 +735,7 @@ alert(soldiers[0].age); // 20 alert(soldiers[1].age); // 23 ``` -If in the example above we used `users.filter(army.canJoin)`, then `army.canJoin` would be called as a standalone function, with `this=undefined`, thus leading to an instant error. +For the example above, the army isn't necessary. canJoin is being called in the context of army, so the this keyword is defined. A call to `users.filter(army.canJoin, army)` can be replaced with `users.filter(user => army.canJoin(user))`, that does the same. The latter is used more often, as it's a bit easier to understand for most people.