soma.js
Version:
soma.js is a javascript framework created to build scalable and maintainable applications.
79 lines (76 loc) • 2.18 kB
JavaScript
soma.applyProperties = function(target, extension, bindToExtension, list) {
if (Object.prototype.toString.apply(list) === '[object Array]') {
for (var i = 0, l = list.length; i < l; i++) {
if (target[list[i]] === undefined || target[list[i]] === null) {
if (bindToExtension && typeof extension[list[i]] === 'function') {
target[list[i]] = extension[list[i]].bind(extension);
}
else {
target[list[i]] = extension[list[i]];
}
}
}
}
else {
for (var prop in extension) {
if (bindToExtension && typeof extension[prop] === 'function') {
target[prop] = extension[prop].bind(extension);
}
else {
target[prop] = extension[prop];
}
}
}
};
soma.augment = function (target, extension, list) {
if (!extension.prototype || !target.prototype) {
return;
}
if (Object.prototype.toString.apply(list) === '[object Array]') {
for (var i = 0, l = list.length; i < l; i++) {
if (!target.prototype[list[i]]) {
target.prototype[list[i]] = extension.prototype[list[i]];
}
}
}
else {
for (var prop in extension.prototype) {
if (!target.prototype[prop]) {
target.prototype[prop] = extension.prototype[prop];
}
}
}
};
soma.inherit = function (parent, obj) {
var Subclass;
if (obj && obj.hasOwnProperty('constructor')) {
// use constructor if defined
Subclass = obj.constructor;
} else {
// call the super constructor
Subclass = function () {
return parent.apply(this, arguments);
};
}
// set the prototype chain to inherit from the parent without calling parent's constructor
var Chain = function(){};
Chain.prototype = parent.prototype;
Subclass.prototype = new Chain();
// add obj properties
if (obj) {
soma.applyProperties(Subclass.prototype, obj);
}
// point constructor to the Subclass
Subclass.prototype.constructor = Subclass;
// set super class reference
Subclass.parent = parent.prototype;
// add extend shortcut
Subclass.extend = function (obj) {
return soma.inherit(Subclass, obj);
};
return Subclass;
};
soma.extend = function (obj) {
return soma.inherit(function () {
}, obj);
};