Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module Pattern in Coffeescript with hidden Variables

Digging into Coffeescript I am trying to port my Javascript files to Coffeescript.

Concerning this, I have a question related to the module pattern of Doulgas Crockford (closure binding in order to keep variables "private")

Therefore my question is: What would the aquivalent Coffeescript for the following JS look like:

var test = function () { var hidden = 'open'; return { open: hidden }; }();

Respectively, is there a different / better aproach to this pattern in Coffeescript?

like image 705
R. G. Avatar asked May 24 '11 08:05

R. G.


People also ask

What is the revealing module pattern?

The Revealing Module pattern is a design pattern for Javascript applications that elegantly solves this problem. The central principle of the Revealing Module pattern is that all functionality and variables should be hidden unless deliberately exposed.

What is the main advantage of using the module pattern in JavaScript?

The module pattern in JavaScript is one of the most used design patterns, used for keeping groups of code independent from each other. Modules allow us to break up different parts of our code to make it easier to maintain and understand. Thus enabling us to keep units of our code clean, separated, and organized.

What is the module pattern in JavaScript?

The Module Pattern is one of the important patterns in JavaScript. It is a commonly used Design Pattern which is used to wrap a set of variables and functions together in a single scope. It is used to define objects and specify the variables and the functions that can be accessed from outside the scope of the function.


2 Answers

I think the best approach is to literally translate your example into CoffeeScript, with the help of the do keyword (which exists mainly to capture values in loops—see my PragPub article):

test = do ->
  hidden = 'open'
  open: hidden

This compiles to

var test;
test = (function() {
  var hidden;
  hidden = 'open';
  return {
    open: hidden
  };
})();

which is identical to your code other than formatting. (The CoffeeScript compiler automatically puts all var declarations at the top of their scope, which makes it easy to determine how a variable is scoped by looking at the JavaScript output.)

like image 183
Trevor Burnham Avatar answered Oct 04 '22 04:10

Trevor Burnham


I added a section to the coffeescript wiki on how I handle namespacing. It's pretty elegent ( I think )

https://github.com/jashkenas/coffee-script/wiki/Easy-modules-with-coffeescript

Coffeescript does not have a native module system above that of enclosing all source code files in an anonymous function. However with a bit of simple trickery you can have modules that are the envy of Ruby. I define my modules like below

@module "foo", ->
    @module "bar", ->
        class @Amazing
            toString: "ain't it"

The implementation of the module helper is

window.module = (name, fn)->
  if not @[name]?
    this[name] = {}
  if not @[name].module?
    @[name].module = window.module
  fn.apply(this[name], [])

which you can put in another source file if you like. You can then access your classes by namespaced modules

x = new foo.bar.Amazing

wrt to your specific question I think the below jasmine spec answer it using my module system

@module "test", ->
  hidden = 10
  @open  = hidden

describe "test", ->
  it "has no hidden", ->
    expect(test.hidden?).toEqual false

  it "has  open", ->
    expect(test.open?).toEqual true
like image 20
bradgonesurfing Avatar answered Oct 04 '22 02:10

bradgonesurfing