I saw this pattern:
Money = (function() {
function Money(rawString) {
this.cents = this.parseCents(rawString);
}
});
in this CoffeeScript screencast preview. (The homepage for the screencast is here.)
Now, I don’t understand this pattern. There is a Money function that contains a Money function. What’s that about?
Could someone explain?
As quoted, there’s no point to that pattern other than that the outer
Moneysymbol can be deleted from thewindowobject (except on IE7 and below, but that’s another story) because it’s a normal (implicit) property ofwindow(as opposed to avaror a symbol deriving from a function declaration). But even then, the outerMoneysymbol receives a function that does absolutely nothing. Could it be misquoted?For instance, here’s a fairly standard patttern:
That’s the module pattern, and it lets you have completely private variables and functions (both illustrated) whilst only having one public symbol. But I’ve had to edit a fair bit to create that (the most significant edits being the
return Money;at the end and the addition of()after the anonymous function so we’re calling it rather than just defining it.