JavaScript Class Inheritance
My JavaScript Class Inheritance notation EX.
by FiNGAHOLiC
HTML
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<ul id="log"></ul>
JavaScript
(function(window, document, undefined){
// http://blog.masuidrive.jp/index.php/2010/02/15/inherited-javascript/
var log = (function(){
var ul = document.querySelector('#log');
return function(mes){
var li = document.createElement('li');
li.innerHTML = mes;
ul.appendChild(li);
};
})();
var extend = (function(){
var F = function(){};
return function(C, P){
F.prototype = P.prototype;
C.prototype = new F();
C.prototype.constructor = C;
C.baseConstructor = P;
C.superClass = P.prototype;
};
})();
var Parent = function(){
this.initialize.apply(this, arguments);
};
(function(){
this.initialize = function(name){
this.name = name || 'NONAME';
};
this.say = function(){
log('I\'m a father.');
log(this.name);
};
}).apply(Parent.prototype);
var Child = function(){
Child.baseConstructor.apply(this, arguments);
};
extend(Child, Parent);
Child.prototype.say = function(){
log('I\'m a child.');
log(this.name);
};
var dad = new Parent('SHIGEO');
var kid = new Child('KAZUSHIGE');
dad.say();
kid.say();
})(this, this.document);