I'm studying Coffeescript's output to try and get a solid understanding of what's happening behind-the-scenes. When I declare a new class Person, a variable named Person is created containing an IIFE.
var Person;
Person = (function() {
function Person() {
this.doThing();
}
Person.prototype.doThing = function() {};
return Person;
})();
What's throwing me here, is that the IIFE itself contains a named function declaration called Person. Does this overwrite the original Person variable, or does Javascript consider this a new scope? When doThing is added to Person's prototype, which object is that specifically referring to?
Perhaps my question betrays a deeper confusion :)
The IIFE, being a function, creates a new scope so that all of the 'class'
logic is nicely hidden away. The Person
inside the IIFE is returned and assigned to the Person
outside of it, but if the IIFE returned something else, then that is what the outer Person
would be.
doThing
is assigned to the prototype Person
inside, but again since Person
is returned, it is the same function object references inside the IIFE.