JS小道具庫

1526 ワード

<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>