JS小小工具库

<script type="text/javascript">
	var dsy = {
		ID : function(_) {return document.getElementById(_);},
		N : function(_) {return document.getElementsByName(_);},
		create : function() { return function() { this.initialize.apply(this,arguments);}},
		extend : function(destination,source) { for(var properties in source) destination[properties] = source[properties];},
		bind : function(obj,fun) { return function() {fun.apply(obj,arguments);}},
		each : function(list,fun) {for(var i = 0,len = list.length; i < len; i++) fun(list[i],i)}
	}
	window.onload = function() {
		alert("test");
		alert(dsy.ID("test"));//测试ID方法是否正常
		alert(dsy.N("test")); //测试N方法是否正常
		var test = dsy.create();
		alert(typeof test);
		/*测试继承*/
		var obj = {'name' : 'testname'};
		dsy.extend(test,obj);
		alert("extends after : "+test.name);
		
		//测试绑定
		var obj1 = {'n':1,'s':'mm'}
		var testFun = function() { alert(this.n +'='+ this.s);}
		var fn  = dsy.bind(obj1,testFun);
		alert("bind after : " + fn());
		
		//测试each
		var testFun = function(current) { alert(current);}
		var list = [
			'a',
			'b',
			dsy.ID('test')
		];
		try {dsy.each(list,testFun);} catch(e) {alert(e.message);}
	}
</script>

 

你可能感兴趣的:(JavaScript)