日期:2014-05-16  浏览次数:20306 次

编写一个JS框架
js 代码:

(function(){
	var window=this,
	_$=window.$,
	
	myJs = window.myJs = window.$ = function(selector) {
		return new myJs.fn.init(selector);
	};
	
	myJs.fn = myJs.prototype = {
		init: function(selector) {
			var dom = document.getElementById(selector);
			this[0] = dom;
		}
	};
	
	myJs.fn.init.prototype = myJs.fn;

	myJs.extend = myJs.fn.extend = function(obj, prop) {
	   if (!prop) {
		  prop = obj;
		  obj = this;
	   }
	   for(var o in prop){
	      obj[o]=prop[o];
	   }
	};
	
	myJs.fn.extend({
		test: function() {
			alert("123");
		},
		get: function() {
			alert(this[0].innerHTML);
		},
		ajax: function() {
		
		},
		ajaxSetting: function() {
		
		},
		
		html: function() {
			alert("html");
		}
	
	});
})();





















页面测试代码:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head>  
    <title>Test Jsonp</title>  
	<script type="text/javascript" src="myjs.js"></script>  
</head>  
<body>  
<div id="cont">Hello, myJs.</div>
<script type="text/javascript">  
	//$.html();
	$("cont").get();
</script>  
</body>  
</html>
















基于这种设计,我们可以十分方便的进行扩展