Created
October 8, 2014 08:05
-
-
Save loosechainsaw/6445e6d327ec7f5c2795 to your computer and use it in GitHub Desktop.
javascript oop / mixin framework
This file contains hidden or 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
j = { | |
context : {} | |
}; | |
j.namepsace = (function(){ | |
var createnamespace = function(parent, remainder){ | |
if(remainder.length === 0){ | |
return; | |
} | |
parent[remainder[0]] = parent[remainder[0]] || {}; | |
return createnamespace(parent[remainder[0]], remainder.splice(1)); | |
}; | |
return { | |
define : function(ns){ | |
createnamespace(j.context, ns.split('.')); | |
} | |
} | |
}()); | |
j.class = (function(){ | |
return { | |
construct : function(namespace, name, ctor){ | |
namespace[name] = ctor; | |
}, | |
ctor : function(namespace, name, ctor, prototype){ | |
for (var property in prototype) { | |
if (prototype.hasOwnProperty(property)) { | |
ctor.prototype[property] = prototype[property]; | |
} | |
} | |
namespace[name] = ctor; | |
} | |
} | |
}()); | |
j.mixin = (function(){ | |
return { | |
with : function(object, objectliteral){ | |
for (var prototype in objectliteral) { | |
if (objectliteral.hasOwnProperty(prototype)) { | |
object[prototype] = objectliteral[prototype]; | |
} | |
} | |
}, | |
toDefintion : function(classdefinition, mixin){ | |
var objectliteral = mixin(); | |
for (var property in objectliteral) { | |
if (objectliteral.hasOwnProperty(property)) { | |
classdefinition.prototype[property] = objectliteral[property]; | |
} | |
} | |
} | |
}; | |
}()); | |
j.namepsace.define('application.section'); | |
j.class.construct(j.context.application.section,'printNameMixin', function(){ | |
return { | |
printName : function(){ | |
console.log(this.name); | |
} | |
}; | |
}); | |
j.class.construct(j.context.application.section,'person',function(name, address, age){ | |
return { | |
name : name, | |
address : address, | |
age : age | |
}; | |
}); | |
j.class.ctor(j.context.application.section,'customer',function(name){ | |
this.name = name; | |
}, { | |
name : 'unregistered client' | |
}); | |
j.mixin.toDefintion(j.context.application.section.customer, j.context.application.section.printNameMixin ); | |
var person = j.context.application.section.person('Blair Davidson','23 Test St',33); | |
var cust = new j.context.application.section.customer("Bankwest"); | |
cust.printName(); | |
console.log(person); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment