I was expecting the obj to be instanceof F1 (‘this’ in ‘this.prop’ refers to F1 object in the code snippet below, similarly I thought ‘this’ in ‘return this’ of C1 refers to F1 which is not the case. It refers to Global Window object), but in actuality it is an instanceof Window. Why is that ? could you please explain ?
function F1() {
this.prop = 5;
function C1() {
return this;
}
return C1();
}
var obj = new F1();
if you are just creating an instance of
F1, then just do this:you don’t need to return anything.
the
thisinsideC1is not the same asthisoutside it. if you want to preservethisof the outside so thatC1can use it, store it into another variable