자바스크립트 prototype을 활용한 상속
(function ($_) {
$_.Base = {};
function subclass() {};
var Class = function(/*parent, protoProps, staticProps*/) {
var arg = $_.A.toArray(arguments), parent = null, protoProps, staticProps;
if($_.O.isFunction(arg[0]))
parent = arg.shift();
protoProps = arg[0];
staticProps = arg[1];
function child() {
var self = this, key, arg = $_.A.toArray(arguments);
for(key in self){
if(!$_.O.isFunction(self[key]))self[key] = $_.O.clone(self[key]);
}
function initsuper(parent) {
if(!parent) return;
if(parent.superclass)
initsuper(parent.superclass);
parent.prototype.initialize.apply(self, arg);
}
initsuper(parent);
this.initialize.apply(this, arg);
}
child.superclass = parent;
child.subclasses = [];
if(parent){
subclass.prototype = parent.prototype;
child.prototype = new subclass();
parent.subclasses.push(child);
}
child.prototype.initialize = function () {};
if (protoProps)
$_.O.extend(child.prototype, protoProps);
if (staticProps)
$_.O.extend(child, staticProps);
child.extend = $_.F.wrap($_.O.extend, function (fn, obj){
var extended = obj.extended;
fn(child, obj);
if(extended) extended(child);
});
child.include = $_.F.wrap($_.O.extend, function (fn, obj){
var included = obj.included;
fn(child.prototype, obj);
if(included) included(child);
});
child.prototype.constructor = child;
return child;
};
$_.O.extend($_.Base, {
Class: Class
});
})(Asdf);
댓글
댓글 쓰기