Created
October 9, 2014 04:48
-
-
Save youngershen/9592b746519ba175d961 to your computer and use it in GitHub Desktop.
This file contains 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
// author : youngershen | |
// email : younger.x.shen@gmail.com | |
// | |
var SUPERCLASS = function(){}; | |
(function(SUPERCLASS){ | |
SUPERCLASS = SUPERCLASS || {}; | |
var CLASS = SUPERCLASS; | |
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); | |
} | |
}; | |
CLASS.extend = function(prop){ | |
var this_super = {}; | |
var initializing = false; | |
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) | |
}); | |
} | |
} | |
} | |
} | |
}; | |
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 if((typeof prop[property] == 'function')){ | |
prototype[property] = prop[property]; | |
} | |
} | |
METACLASS.extend = arguments.callee; | |
METACLASS.prototype = prototype; | |
METACLASS.constructor = METACLASS; | |
supertype = null; | |
prototype = null; | |
return METACLASS; | |
}; | |
} | |
})(SUPERCLASS); | |
var Person = SUPERCLASS.extend({ | |
init:function(name,age){ | |
this.name = name; | |
this.age = age; | |
}, | |
say:function(){ | |
console.log(this.name); | |
console.log(this.age) | |
} | |
}); | |
var Student = Person.extend({ | |
init:function(name, age, id){ | |
debugger; | |
this.super.init(name, age); | |
this.id = id; | |
}, | |
say:function(){ | |
this.super.say(); | |
console.log(this.id); | |
}, | |
get_id:function(){ | |
console.log(this.id); | |
} | |
} | |
); | |
var GoodStudent = Student.extend({ | |
init:function(name, age, id, score){ | |
console.log(this.super.init); | |
debugger; | |
this.super.init(); | |
this.score = score; | |
}, | |
score:function(){ | |
conosole.log(this.score); | |
} | |
}); | |
var p = new Person('younger', '28'); | |
console.log(p.name); | |
console.log(p.age); | |
p.say(); | |
console.log(p); | |
/* | |
var s = new Student('lily', 15, '123456'); | |
s.say(); | |
s.get_id(); | |
console.log(s) | |
*/ | |
var gs = new GoodStudent('hehe', 11, '123', 100); | |
console.log(gs); | |
gs.score(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment