thenewtricks/courage.js — pull request #5 by tylrtrmbl (2014-09-18 06:08:14+00:00)
Title: Big Style Changes

### Use different “private” marker for variables

Before we used a `_private` object on the main object to hide private vars:

```js
var Class = function Class() {

this.publicVar = 'hi';

this._private = {
privateVar1: 'go away',
privateVar2: 'i am sleeping',
}
};
```

We accessed them inside methods via a "`my`" construct:

```js
function method() {

var my = this._private;

my.privateVar = 'hissss';
}
```

Problems?

- Was not very JS-like, and so mildly confusing
- Didn't prevent access to the vars, since you can't
- Was a bit of a bear on file size

So I changed them to another JavaScript style I'm used to seeing: private variables begin with an `_`.

Constructors:

```js
var Class = function Class() {

this.publicVar = 'hi';

this._privateVar1 = 'go away',
this._privateVar2 = 'i am sleeping',
};
```

Methods:

```js
function method() {
this._privateVar1 = 'hissss';
}
```

I think these look nicer, more expected, and don't sacrifice knowing how "private" something is.

I did something similar with classes.

Before:

```js
var PrivateCourage = Courage._private = Courage._private || {};
PrivateCourage.PrivateClass = ...
```

After:

```js
Courage._PrivateClass = ...
```

### Use short-form prototype declarations

Before:

```js
Courage.Class.prototype = {

pubMethod1: function pubMethod1() {
...
}

pubMethod2: function pubMethod2() {
...
}
}

privateMethod: function privateMethod() {
...
}
```

Private "methods" were just called directly, using the functions directly with the `.bind()` syntax to give access to the object.

Now:

```js
Courage.Class.prototype = {

pubMethod1: pubMethod1,
pubMethod2: pubMethod2,

_privateMethod: privateMethod,
}

function pubMethod1() {
...
}

function pubMethod2() {
...
}

function privateMethod() {
...
}
```

This has 2 distinct advantages:

- The prototype makes it easy to tell which methods a class defines
- Cuts down on overuse of the `.bind()` syntax (which should have been a `.call()` anyway)
- Bind is now used only where it belongs: binding methods to their object for callbacks and timers

This method makes use of function hoisting to keep functions out of the way. You can really see all the properties and methods an object has at the top of the file now.

The only arguable downside I can see now is that private functions (really methods) are now callable outside the module; but I think the benefits outweigh the downside.

### Minor Connection Manager change

- With the above change, calling a method in the constructor was odd
- Created separate public “start” method the caller must invoke

This is 2 step initialization, but I'm a fan in this case.

### I also added function hoisting in `UUID.js`

Similar to the other benefits, you now see all the functions in UUID.js at the top.

## Closing Notes

Do you care about these changes? I implemented them based on some stuff I learned taking JavaScript classes at Google. They may be funky.

Also, there's a lot of changes all squooshed together here. Sorry, I got excited. It's worth the effort to match each change in the diff to one of the headings above, but sorry I made it that way.

## Tested?

Smoke tested with courage-web-demo, since there were no public API changes.