-
-
Save phiggins42/894544 to your computer and use it in GitHub Desktop.
dojo.provide("Foo"); // always first | |
dojo.require("All.Your.Code.Are"); // always second | |
dojo.require("Belong.To.Us"); | |
(function(d){ // optional wrapper / privatizer / closure | |
// privates for this module | |
var yep = true, | |
nope = false, | |
helperUpper = function(str){ | |
return str.toUpperCase() | |
} | |
; | |
// declarations | |
dojo.declare("MyTHing", [inheritanceChain], { | |
member1: "Public members listed first in declcartion", | |
anumber: 42, | |
etc: "and so on", | |
// private members, too: | |
_test: 1, | |
_test2: 3, | |
_test5: "etc", | |
// begin lifecycle overrides | |
postMixInProperties: function(){ | |
dojo.mixin(this, { something:"random" }); | |
this.connect(this.domNode, "click", "awesomeFunction"); | |
this.inherited(arguments); | |
}, | |
// and so on | |
// begin public class members | |
awesomeFunction: function(e){ | |
this.foobar = this.oneMore("time"); | |
}, | |
_privateHelperForAwesomeFunction: function(e){ | |
// should be immediately following the function it helps | |
}, | |
anotherAwesomePublicFunction: function(){}, | |
oneMore: function(arg){ | |
return helperUpper(arg); | |
}, | |
// unless it's super generic, then it goes at the bottom. | |
_count: function(f){ | |
} | |
}); | |
})(dojo); |
also because the var yep = true; is a local variable, it is safely obfuscated to _1 or some such by shrinksafe/closure/yuicompressor/etc ... so even setting yep
in a different module/anywhere wouldn't be able to guess at the localized var
wow that's awesome. I was going to make that argument too :)
so they are a little bit like constants in the sense that even MyTHing can't change their values?
well, not constants
so much as they can be changed by public api's ... (anything exposed off of MyTHing
won't be obfuscated) and the internal references there will be updated to match whatever the initial declaration was ... but def. semi-private defaults. I use the pattern lots.
(function(){
var defaults = { thing:"er", foo: 1 };
dojo.NodeList.prototype.coolPlugin = function(args){
var opts = dojo.mixin({} /* new literal */, defaults, /* static obj */, args /* user defined */);
// now use opts.thing, opts.foo. if they passed args.foo, the args wins
});
})();
could you, for example, arrange it so that properties are private but you have public setters/getters? Or once the closure fires the variable is accessible but cannot be updated?
not with es3 ... which is everywhere. modern browsers you could use defineGetter et al and accomplish this, but I'm a pragmatist and also still have to support ie6 in most things, so it's more of a pipe dream.
nope, that yep would be a global. the first setYep would complain about shadowing a local var. you'd have to somehow be in the closure scope to redefine that
yep