Last active
August 29, 2015 13:57
-
-
Save joseph/9771701 to your computer and use it in GitHub Desktop.
JS class pattern [4]
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(); | |
}, PROTO = ClassName.prototype; | |
// A constant | |
PROTO.MEANING_OF_LIFE = 42; | |
// A "private" method begins with an underscore: | |
PROTO._initialize = function () { | |
} | |
// A public method does not begin with an underscore: | |
PROTO.update = function () { | |
} | |
return ClassName; | |
}); |
yeah, I'm still not too keen on K due to that penalty...most editors allow for auto complete of ClassName.prototype and I don't mind the verbosity, it's one less thing to track in your head, etc...but once again due to it being a single "class" per file, it doesn't bother me much
I played around with K
as $
, which seems less idiosyncratic, and happily it should annoy jQuery acolytes, but it felt a bit Perl-like. Currently I'm using PROTO
for K
— we'll see how long it lasts. I never use auto-complete and I don't mind typing shit out; I'm just trying to minimize repetitive line noise for ready readability.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very slight variation on pattern [3], where the prototype is assigned to
K
so that we're not constantly repeatingClassName.prototype
. Also the constants are now assigned to the prototype rather than the class itself — this may be good for inheritance, but I haven't really figured it through.It should minify more nicely, but the
K
possibly introduces a bit of a comprehension penalty for new devs.