Skip to content

Instantly share code, notes, and snippets.

@jtenner
jtenner / gist:7374869
Created November 8, 2013 17:51
The innner workings of the plugin architecture
plugins = [];
//inside of NovelScript.js
NovelScript["plugin"] = function(func){
plugins.push(func);
}
//inside of the NovelScript function constructor
plugins.forEach(function(plugin){
plugin(self, options);
@jtenner
jtenner / gist:7375356
Created November 8, 2013 18:26
Basic library constructor definition exposed to the global namespace via the window parameter
!function(window, document, undefined){
function constructorObject(options){
//do something with constructor object here
}
window["constructorObject"] = constructorObject;
}(window,document)
!function(window, document, undefined){
plugins = [];//Plugin storage object
function constructorObject(options){//object constructor
var self = this; //store itself for later
plugins.forEach(function(plugin){
plugin(self, options);//pass this instance of the constructed object to the plugin
//the options used to construct it are also passed as well
plugins.forEach(function(plugin){
plugin(self, options);//pass this instance of the constructed object to the plugin
//the options used to construct it are also passed as well
});
!function(constructorObject, undefined){
constructorObject.plugin(function(self, options){
//Plugin constructor code goes here
//Now you have access to the self object and options by reference
if(typeof options !== "undefined" && options.property)//put some logic here...
self.protofunc();//call a function on itself
});
constructorObject.prototype.protofunc = function(){// in the closure space, modify it's prototype
console.log("hello world!");
var myObject = new constructorObject;
myObject.protofunc();
!function(constructorObject){
constructorObject.plugin(function(self, options){
var events = {};//event parent
self.on = function(eventNameSpace, func){//add the function to the namespace
var event = events[eventNameSpace] = events[eventNameSpace]||[];//coalesce and make sure it's an array
func._function_id = func.name+Date.now();//give it some unique value for later
event.push(func);//store it
return self;//chain!
!{
//Typical API functions should go here
//func : function(){}....
//func2 : function(options){...}
//Object creation methods...
//convenience methods here for create and expose via this
createObject: function(options){
//constructor logic here
var created = Object.create(this.fn);
~{
func: function(){
alert("hello world!")
}
}.func();
var foo = {
bar: {
//put some named functions here
},
baz: function(){
return Object.create(this.bar);
}
}.baz();