0

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.

Pollob
  • 29
  • 1
  • 4
  • This has absolutely nothing to do with "JSON", nor "server" or "client" side.. – Cerbrus Sep 14 '17 at 09:26
  • Yes. I also think so. I said **Seems like** because I meant conceptually. I am not sure whether this question really is a duplicate of that question. All the examples in that question have a global variable. In my example, `InnerScope` is not global/outer scope. That's where the confusion is. **How does JS interpreter recognises the Type of the object?** – Pollob Sep 14 '17 at 09:36
  • 1
    You're returning a new instance of the `InnerScope`. That instance has those properties... – Cerbrus Sep 14 '17 at 09:38

0 Answers0