【メモ】「js権威の手引き」-第9章類とモジュール-9.7サブクラス


1.サブクラスを定義する:
Function.prototype.extend = function(constructor, method, statics) {
	return defineSubclass(this, constructor, method, statics);
};

//    (    ),   setter getter
function extend(o, p) {
	for (prop in p) {
		o[prop] = p[prop];
	}

	return o;
}

function inherit(p) {
	if (p == null)
		throw TypeError();
	if (Object.create)
		return Object.create(p);
	//  
	var t = typeof p;

	if (t !== "object" && t !== "function")
		throw TypeError();

	function f() {
	};
	f.prototype = p;
	return new f();
}

function defineSubclass(superclass, constructor, method, statics) {
	constructor.prototype = inherit(superclass.prototype);
	constructor.prototype.constructor = constructor;
	//        
	if (method)
		extend(constructor.prototype, method);
	if (statics)
		extend(constructor, statics);
	return constructor;
}
//      
function ClassA(paramA, paramB) {
	//      
	this.propA = paramA;

	this.getParamB = function() {
		return paramB;
	};
	this.setParamB = function(value) {
		paramB = value;
	};
}

//      
ClassA.prototype.funcA = function() {
	this.setParamB(123);
	return this.getParamB();
};

//     (  )
ClassA.STATIC_CONSTA = 1000;

//     
ClassA.staticFuncA = function() {
};

//     
ClassA._privateProp = 1;
var ClassB = ClassA.extend(function ClassB(paramA, paramB, paramC) {
	//super      
	ClassA.apply(this, arguments);
	this.porpC = paramC;
}, {
	funcB: function (){return " this is funcB";},
	//             
	funcA: function () {
		return "this is funcA's result in ClassB: " + ClassA.prototype.funcA.apply(this, arguments);
	}
});