this my code:
obj={
obj1:{
funcTest1a:function(){ console.log ("test _1a: ",this)},// this return obj1
funcTest1b:()=>console.log("test _1b: "+ this), // this return windows
obj2:{
funcTest2a:function(){ console.log ("test _2a: ",this)},// this return obj1
funcTest2b:()=>console.log("test _2b: "+ this), // this return windows
},
obj3:{
test3(){
console.log(this);
obj.obj1.obj2.funcTest2b()
},
}
}
}
I understand that if I call in the console :
obj.obj1.obj2.funcTest2b()
return window that is ok
but
If I call
obj.obj1.obj3.test3()
inside this function test3() I call
obj.obj1.obj2.funcTest2b()
in which case I expected "THIS" to be different.
a possible return from an arrow function a this that is not window or global?
> I find a solution o better an answer
this is an example of how to use arrow function where THIS is not a global
a = {
hello: 'hola',
b() {
return () => {
console.log(this.hello) //or this;
}
}
}
a.b()() // print hola
so in my code, I have to change:
funcTest2b:()=>console.log("test _2b: "+ this)
in
funcTest2b: function(){
let testInside=()=>console.log("test _2b: "+ this)
testInside()
}