I am not able to understand the following code,
function Foo(arg) {
if (!(this instanceof Foo)){
alert("inside if statement");
return new Foo(arg);
}
else{
alert("inside else statement")
}
this.myArg = arg;
}
let foo_obj = Foo("bar");
alert(foo_obj.myArg);
When the program is executed, when if statement is first encountered, it checks whether "this" (which is the window object then) has "Foo" in its prototype chain, since it is not there the following is executed
return new Foo(arg);
So Foo is called again, but this time the "else" part is executed. Why is that so? What is the value of "this" the second time?