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.

Abierto
#2,813 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
HTML
Estrellas
25.5k
Forks
4k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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);

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

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.】【。

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript
Área
documentation
Tipo de issue
Documentación
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.