Last active
August 29, 2015 14:04
-
-
Save bentedder/be2e0283f0d26f6fcde5 to your computer and use it in GitHub Desktop.
basic js pattern (with requirejs)
This file contains 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
require(["thing"], function(Thing) { | |
var thing = new Thing({ | |
place: "Chicago" | |
}); | |
thing.runAround(); | |
}); |
This file contains 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
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.