Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's up with this JavaScript pattern?

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?

like image 660
Šime Vidas Avatar asked May 13 '11 21:05

Šime Vidas


People also ask

Why do we need design patterns in JavaScript?

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 JavaScript pattern?

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.

Do design patterns apply to JavaScript?

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”.


2 Answers

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.

like image 110
T.J. Crowder Avatar answered Oct 09 '22 02:10

T.J. Crowder


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).

like image 28
RightSaidFred Avatar answered Oct 09 '22 00:10

RightSaidFred