I'm learning javascript : I try to access "this" in the function "checkAndChange" but, according to how I declare and export my function , "checkAndChange" can't call previous functions (this.isErr) because sometimes this refers to "global".
I try it with 3 ways , 1 works but i don't know why. This is an example :
exports.isErr = (err) => {
return err instanceof Error;
}
// This way works :
exports.checkAndChange = (obj) => {
console.log(this); // Here , "this" contains "isErr".
if (this.isErr(obj))
return "Error";
else
return "It's Okay"
}
function checkAndChange(obj){
console.log(this); // But not here , "this" refers to "global".
if (this.isErr(obj)) // TypeError: this.isErr is not a function
return "Error";
else
return "It's Okay"
exports.checkAndChange = checkAndChange; // here i have to bind(this) to make it work and i don't know why.
}