In the following example I have a class and add two functions by prototype the first as defined by funcion normalmente
, while the second with arrow function
simpleClase = function(){
this.ejemplo = 1
}
simpleClase.prototype.simple = function(){
console.log(this)
}
simpleClase.prototype.arrow = () =>{
console.log(this)
}
let nuevaClase = new simpleClase();
nuevaClase.simple();
nuevaClase.arrow();
The fact is that for the function as it is normally defined with prototype, it returns the contexto de la clase
, while using arrow function
it returns window
With another example:
class Clase {
constructor(){
this.valor = 1
}
arrow() {
let miFuncion= () => console.log("arrow" , this);
miFuncion();
}
simple(){
console.log("simple" , this);
}
}
let miClase = new Clase();
miClase.arrow()
miClase.simple()
It gives me back the context of the class, this gives me the doubt of how prototype works with arrow function my doubt is because these areas are different, because the prototype does not takes the scope of the variable