In the following example I have a class and add two functions by prototype , the first one as defined funcion normalmente
, while the second one witharrow 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 case is that for the function as normally defined with prototype it returns me the contexto de la clase
, while using arrow function
it returns mewindow
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 returns the context of the class, this gives me the doubt of how prototype works with the arrow function , my doubt is because these scopes are different, because the prototype does not take the scope of the variable