Skip to content

Instantly share code, notes, and snippets.

@joseph
Created March 25, 2014 21:18
Show Gist options
  • Save joseph/9771613 to your computer and use it in GitHub Desktop.
Save joseph/9771613 to your computer and use it in GitHub Desktop.
JS class pattern [3]
// AMD module definition:
define(function (require) {
// An instantiable class:
var ClassName = function () {
// All private instance data is stored in this._
this._ = {};
this._initialize();
};
// A constant
ClassName.MEANING_OF_LIFE = 42;
// A "private" method begins with an underscore:
ClassName.prototype._initialize = function () {
}
// A public method does not begin with an underscore:
ClassName.prototype.update = function () {
}
return ClassName;
});
@joseph
Copy link
Author

joseph commented Mar 25, 2014

The nice thing about this is that with a real class name — rather than the anonymous standard K — it is easier to inspect in the console. It's still anonymous enough that things can reassign it on require. There is some verbosity in all the ClassName.prototype prefixes.

Putting all 'private' instance data in this._ seems quite nice — the underscore is more suited to this than p.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment