JAVASCIRPT继承与命名空间

<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<body>
	<div id="contain1">
		<input type = "button" name="test1"  option="{required:true}">
	</div>
    <div id="contain2">
		<input type = "button" name="test2"  option="{required:true,type:'number'}">
	</div>
</body>
<script type="text/javascript">
<!--
//声明一个对象$.problemWo,添加一个b的静态属性和myclick的静态方法。
(function ($) {
    	 // 命名空间的绑定在$上  
    $.namespace = function() {  
		var a=arguments, o=null, i, j, d;    
		for (i=0; i<a.length; i=i+1) {        
			d=a[i].split(".");  
			if(!$.com){
				o=$.com = function(){};  
			} else {
				o = $.com;
			} 
			for (j=(d[0] == "com") ? 1 : 0; j<d.length; j=j+1) {            
				o[d[j]]=o[d[j]] || {};            
				o=o[d[j]];        
				}    
			}    
			return o;
	};
	//注册命名空间
	$.namespace("com.frame");
	//为类增加静态方法
	$.com.frame.extendStatic = function (obj,obj1){
		if(obj && obj1){
			$.extend(obj,obj1);
		}
	}
	$.com.frame.extend= function (classObj,obj1){
		var tempObj = function(){};
		if(classObj && obj1){
			if(typeof(classObj) == "function" && typeof(obj1) == "object"){
				$.extend(classObj.prototype,obj1);
				tempObj.prototype = new classObj();
			}else{
				console.log("请检查参数!");
			} 
		}else if(classObj){
			if(typeof(classObj) == "function"){
				tempObj.prototype = new classObj();
			}
		}else{
			console.log("请检查参数!");
		} 
		return tempObj;
	}
    $.com.frame.Base = function () {
		this.x = "123";
        this.init();
    };
	$.com.frame.Base.prototype =  {
		 init:function(){
			 console.log("init");
		 },
		 bindEvent:function(){
		 },
		 validate:function(){
		 }
    };
})(jQuery);
    
 $.namespace("com.frame.test");
 $.com.frame.test.ProblemWo =  $.com.frame.extend($.com.frame.Base );
 
 $.com.frame.test.ProblemWo1 =  $.com.frame.extend($.com.frame.Base, {
	temp:"2222",
	init:function(){
		this.c = "2222222";
		this.x = "b2bb";
		console.log(this.temp);
		this.temp = "12211";
		console.log(this.c);
		console.log(this.b);
	}
});

//声明一个对象$.problemWo,添加一个b的静态属性和myclick的静态方法。
(function ($) {
	$.com.frame.extendStatic($.com.frame.test.ProblemWo,
	 { staticT: 5 ,staticTest: function () {   } }
	);
})(jQuery);
 var problemWo = new  $.com.frame.test.ProblemWo();
 
  var problemWo1 = new  $.com.frame.test.ProblemWo1();
 alert(problemWo1.x);
 alert(problemWo.x);
 
$("#contain1 input[option]").each(function(){
	alert(this.name);
	var optionObj = this.getAttribute("option");
})

 </script>

猜你喜欢

转载自sxpyrgz.iteye.com/blog/2333139