javascript-tutorial / javascript-tutorial/en.javascript.info

Incorrect information of Data Types/Array Methods about thisArg

Ouverte
#3,766 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
HTML
Étoiles
25.5k
Forks
4k
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

I was reading the article on Data Types, Array Methods when I saw the example code for thisArg. The example code below:
![Example code](https://github.com/user-attachments/assets/de6aabf7-a7d9-40d1-a473-e8dffd849c40)

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par l’article Data Types/Array Methods et repérez l’exemple de thisArg présenté dans l’issue. Vérifiez l’explication par rapport au comportement de l’exemple, puis mettez à jour la formulation pour expliquer correctement le problème et évaluez si une section plus générale sur this est nécessaire. C’est terminé lorsque l’article n’attribue plus l’erreur à la mauvaise cause.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript
Domaine
documentation
Type d'issue
Documentation
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.