Skip to content

Instantly share code, notes, and snippets.

@bentedder
Last active August 29, 2015 14:04
Show Gist options
  • Save bentedder/be2e0283f0d26f6fcde5 to your computer and use it in GitHub Desktop.
Save bentedder/be2e0283f0d26f6fcde5 to your computer and use it in GitHub Desktop.
basic js pattern (with requirejs)
require(["thing"], function(Thing) {
var thing = new Thing({
place: "Chicago"
});
thing.runAround();
});
define(["jquery"], function($) {
var Thing = function(args) {
this.config = this.args;
this.init();
};
// Method 1 (more standard)
Thing.prototype.init = function() {
console.log("app has initialized!");
console.log(this.config);
};
Thing.prototype.runAround = function() {
console.log("I'm running!");
console.log("I'm running to " + this.config.place);
};
// Method 2 (I kind of like this way better)
var methods = {
init: function() {
console.log("app has initialized!");
},
runAround: function() {
console.log("I'm running!");
}
};
// Method 2 just adds all those functions into the prototype. Seems cleaner to me.
$.extend(Thing.prototype, methods);
});
@bentedder
Copy link
Author

Method 1 adds stuff directly to the prototype. But I like method 2 to keep it cleaner. I like working in an object literal w/my functions. Just a preference though.

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