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?
Design patterns are reusable solutions to commonly occurring problems in software design. They are proven solutions, easily reusable and expressive. They lower the size of your codebase, prevent future refactoring, and make your code easier to understand by other developers.
What is a Pattern? A pattern is a reusable solution that can be applied to commonly occurring problems in software design - in our case - in writing JavaScript web applications. Another way of looking at patterns are as templates for how we solve problems - ones which can be used in quite a few different situations.
JavaScript modules are the most prevalently used design patterns for keeping particular pieces of code independent of other components. This provides loose coupling to support well-structured code. For those that are familiar with object-oriented languages, modules are JavaScript “classes”.
As quoted, there's no point to that pattern other than that the outer Money
symbol can be deleted from the window
object (except on IE7 and below, but that's another story) because it's a normal (implicit) property of window
(as opposed to a var
or a symbol deriving from a function declaration). But even then, the outer Money
symbol receives a function that does absolutely nothing. Could it be misquoted?
For instance, here's a fairly standard patttern:
Money = (function() { var someCompletelyPrivateVariable; function doSomethingCompletelyPrivate() { } function Money(rawString) { this.cents = this.parseCents(rawString); } return Money; })();
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.
Using the CoffeeScript code that the video claims is a proper conversion...
class Money constructor: (rawString) -> @cents = @parseCents rawString
...CoffeeScript will generate the following, which is basically identical to @T.J. Crowder's answer:
var Money; Money = (function() { function Money(rawString) { this.cents = this.parseCents(rawString); } return Money; })();
I'm just posting this to show what CoffeeScript actually does, and that the video does not represent the reality.
You can see the conversion if you visit the site and click the "Try CoffeeScript" button.
Please do not "accept" this answer.
EDIT:
To add some private variable usage that utilizes the scope, you could do this:
class Money priv=0 constructor: (rawString) -> @cents = @parseCents rawString @id = priv++
...which renders as:
var Money; Money = (function() { var priv; priv = 0; function Money(rawString) { this.cents = this.parseCents(rawString); this.id = priv++; } return Money; })();
By the way, I know nothing about CoffeeScript. Its syntax looks confusing to me, but perhaps just because I'm not accustomed to it.
I like JavaScript the way it is (especially with the new and yet to come changes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With