I am reading a book and it says that this
inside a function is set in 4 different ways. Default, implicit, explicit binding + using a new
syntax. In all cases, this
is set at function call. Take a look at example.
When I call jajca()
, I am expecting this
to be set via default binding.. thats is when jajca()
is called this
is already set to the object that was created using new
syntax. Yet for some reason I don't understand, this
inside jajca
points to window even though that via default binding, this
should be pointing to the object created using new
.
function jajca() {
this.musicFolderPath = "value2"; // never gets assigned to the correct this.musicFolderPath
}
function Music(){
this.musicFolderPath;
jajca(); // music.musicFolderPath is undefined
// jajca.call(this); music.musicFolderPath is correctly set
}
var music = new Music();
//music.musicFolderPath is undefined
Can you explain why this
is not set as I expected? If explicit binding is used, than this
points to the object created using new
keyword as expected.