Skip to content

Instantly share code, notes, and snippets.

@youngershen
Created October 7, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youngershen/24f3e37faa55eee7d530 to your computer and use it in GitHub Desktop.
Save youngershen/24f3e37faa55eee7d530 to your computer and use it in GitHub Desktop.
// author : youngershen
// email : younger.x.shen@gmail.com
//
var SUPERCLASS = function(){};
(function(SUPERCLASS){
SUPERCLASS = SUPERCLASS || {};
var CLASS = SUPERCLASS;
var initializing = false;
var this_super = {};
var set_builder_func = function(prop){
return function(value){
if(this.super[prop] == undefined){
this[prop + '_value'] = value;
}else if(this[prop] == undefined){
this.super[prop] = value;
}
};
};
var get_builder_func = function(that, prop){
return function(){
return (function(prop){
return function(){
if(that[prop + '_value'] != undefined){
return that[prop + '_value'];
}else{
return that.super[prop];
}
}();
})(prop);
}
};
var METACLASS = function(){
if(initializing && this.init && arguments.length != 0){
this.super = this_super;
this.init.apply(this, arguments);
for(var prop in this){
if((typeof this[prop]) != 'function'){
if(this.super[prop] == undefined && prop != 'super'){
this[prop + '_value'] = this[prop];
}
if(prop == 'super'){
for(var _prop in this[prop]){
if((typeof this[prop][_prop]) != 'function'){
Object.defineProperty(this, _prop,{
enumerable:true,
configurable:true,
set:set_builder_func(_prop),
get:get_builder_func(this, _prop)
});
}
}
}else{
Object.defineProperty(this,prop, {
enumerable:true,
configurable:false,
set:set_builder_func(prop),
get:get_builder_func(this, prop)
});
}
}
}
}
};
CLASS.extend = function(prop){
var supertype = this.prototype;
var prototype = new this();
initializing = true;
if((this instanceof Function)){
for(var property in prop){
if(typeof prop[property] == "function" && typeof supertype[property] == 'function'){
this_super[property] = supertype[property]
prototype[property] = prop[property];
}else{
prototype[property] = prop[property];
}
}
METACLASS.extend = arguments.callee;
METACLASS.prototype = prototype;
METACLASS.constructor = METACLASS;
return METACLASS;
};
};
})(SUPERCLASS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment