Created
March 25, 2014 21:18
-
-
Save joseph/9771613 to your computer and use it in GitHub Desktop.
JS class pattern [3]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 onrequire
. There is some verbosity in all theClassName.prototype
prefixes.Putting all 'private' instance data in
this._
seems quite nice — the underscore is more suited to this thanp
.