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

you pass dojo in as d but never use it, any reason? just illustrating?

@phiggins42
Copy link
Author

force of habit, my bad. yes, inside the closure should be s/dojo/d/gi ... this was just a simple skeletal helper module to show how I kind of organize the components of a module.

@HelloGrayson
Copy link

couldn't client code get to your "private" variables by hooking into MyTHing.

//... your code then:
MyThing.prototype.setYep = function(yep) {
    yep = yep;
}
MyThing.prototype.getType = function() {
    return yep;
}
MyThing.setYep(false);
console.log(MyThing.getYep()); //false

@phiggins42
Copy link
Author

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

@phiggins42
Copy link
Author

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

@HelloGrayson
Copy link

wow that's awesome. I was going to make that argument too :)

@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