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

Incorrect information of Data Types/Array Methods about thisArg

Aperta
#3,766 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
HTML
Stelle
25.5k
Fork
4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Inizia con l'articolo Data Types/Array Methods e individua l'esempio di thisArg mostrato nell'issue. Verifica la spiegazione rispetto al comportamento dell'esempio, quindi aggiorna la formulazione per spiegare accuratamente il problema e valuta se sia necessaria una sezione più ampia su this. Il lavoro è completato quando l'articolo non attribuisce più l'errore alla causa sbagliata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.