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

The arrow function.... The lookup of this is made exactly the same way as a regular variable search: in the outer lexical environment.

Aperta
#2,813 1 commento 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 have tried to test this, i believe arrow function doesn't look up for 'this', in the outer lexical environment, it remembers the 'this' of the place where it is defined.
In the below code: arrow function is passed to another function and that function calls the arrow function at line //**. At line //** 'this' is 'window', so if arrow function looks up for this in outer lexical environment, then 'this' should be window object, not cl_Test.
Hence, i believe the statement put by you in arrow functions revisited should be changed. Please correct me if i am wrong.

    function cl_Test() {

// let name = 'Pradeep';
this.name = 'Pradeep';
this.age = '31';

this.arrowF = () => {
console.log(`${this.name + ' ' + this.age}`);
};
this.normalF = function () {
console.log(name)
};
this.arrowF2 = () => {
console.log(name);
}
};

function fTest(f1) {
f1(); // **
};

let o1 = new cl_Test();
o1.name = 'Sunil';
o1.age = '34';

fTest(o1.arrowF); //*
fTest(o1.normalF);
fTest(o1.arrowF2);

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Start by reading the “arrow functions revisited” section referenced in the issue and compare its statement about how arrow functions obtain this with the supplied example. Verify the behavior of arrowF and normalF when passed to fTest, then update the explanation so it accurately describes lexical this.】【。

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.