jsは対象に基づいて成りすまして原型の方式と継承関係を実現します.

1126 ワード

	Class = function(){
		var subClass = function(){};
		var superClass = arguments[0];
		if(typeof(superClass) === "function"){
			subClass = function(){
				if(this.initialize){
					this.initialize.call(this , arguments);
				}
			};
			subClass.prototype = new superClass();
			subClass.prototype.constructor = subClass;
		}
		subClass.prototype.extend = function(extendsObj){
			for(var m in extendsObj){
				this[m] = extendsObj[m];
			}
		};
		return  subClass;
	};

    SuperClass = Class();
	SuperClass.prototype.extend({
		a : 'superClass field a',
		b : function(){
			return "superClass method b";
		},
		c : 's'
	});

    SubClass = Class(SuperClass);
	SubClass.prototype.extend({
		a : 'subClass field a',
		b : function(){
			return "subClass method b";
		}
	});
	
	var s = new SubClass();
	alert(s.a);
	alert(s.b());
	alert(s.c);