Simple boilerplate I use to start JavaScript projects.
Last active
March 27, 2018 14:59
-
-
Save nessthehero/4730750 to your computer and use it in GitHub Desktop.
JavaScript Boilerplate
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
// We're cooking with globals, so we use namespaces to avoid conflicts | |
var Namespace = Namespace || {}; | |
Namespace.Project = Namespace.Project || {}; | |
Namespace.Project.init = function () { | |
} | |
// Expecting jQuery, but you could easily change this to some other method of window ready or dom ready | |
$(function () { | |
// Cached selectors | |
Namespace.Project.$elm = {}; | |
// Arbitrary data (Comes after selectors so we can use them here) | |
Namespace.Project.data = {}; | |
Namespace.Project.init(); | |
}); |
@raganwald, had no clue about checking undefined with void 0
. I guess you learn something new everyday
I find AngularJS and RequireJS solves namespacing issues quite nicely
@mattgoucher I do that as well. I suppose you could take the solution from @scottbert and just leave the global namespace part uncommented so that it remains in the global namespace.
I like using browserify for this kind of thing. You have to do a static build step and async code loading is pretty much out of the question, but at least those require
s look nice. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to know whether
undefined
has been fucked with, you can write:Similarly, you can set undefined for yourself with:
Or the old classing as part of your IIFE: