Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created March 30, 2011 14:53
Show Gist options
  • Save phiggins42/894544 to your computer and use it in GitHub Desktop.
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);
@HelloGrayson
Copy link

so they are a little bit like constants in the sense that even MyTHing can't change their values?

@phiggins42
Copy link
Author

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
  });
})();

@HelloGrayson
Copy link

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?

@phiggins42
Copy link
Author

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.

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