Consider the following code snippet:
var OuterScope = function () {
function InnerScope () {
this.name = "K";
this.address = 10;
}
return new InnerScope();
};
console.log(new OuterScope());//Returns: InnerScope { name: 'K', address: 10 }
console.log(new OuterScope().constructor.name);//Returns: InnerScope
Why/how does new OuterScope()
returns the data? InnerScope
is not defined in global namespace (where new OuterScope()
is being invoked) yet JS properly recognizes the object/it's properties/constructor name etc.
Seems like returned data is parsed into JSON & JS interpreter in global namespace creates a JS object based on that JSON structure similar to what happens when we feed JSON data to client side from server side.