You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was reading the article on Data Types, Array Methods when I saw the example code for thisArg. The example code below:
The explanation why the code would give a problem if we omit the thisArg argument here is:
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.
The canJoin isn't called as a standalone function. It's been called in the context of army with the dot syntax. Inside canJoin where we use the 'this' keyword, 'this' points correctly to army. The problem here for some reason is the (implicit in that case as we haven't define it) is the user member of the users array. That's what causes the error and not the omission of the thisArg.
I believe that a whole section about how 'this' works would be highly valuable as it's extremely important to know and often a source of bugs.
The text was updated successfully, but these errors were encountered:
anastasiosPou
changed the title
(Maybe) Incorrect information of Data Types/Array Methods about thisArg
Incorrect information of Data Types/Array Methods about thisArg
Oct 4, 2024
I was reading the article on Data Types, Array Methods when I saw the example code for thisArg. The example code below:
The explanation why the code would give a problem if we omit the thisArg argument here is:
The canJoin isn't called as a standalone function. It's been called in the context of army with the dot syntax. Inside canJoin where we use the 'this' keyword, 'this' points correctly to army. The problem here for some reason is the (implicit in that case as we haven't define it) is the user member of the users array. That's what causes the error and not the omission of the thisArg.
I believe that a whole section about how 'this' works would be highly valuable as it's extremely important to know and often a source of bugs.
The text was updated successfully, but these errors were encountered: