JavaScript可以在函数名中传递参数。这样做的好处是:一个函数定义,实际是定义了多个函数。同时,可以把代码相同的函数合并成一个。
实现这一方法的机制其实相当简单,那就是使用所谓的软编码。即用数组模式访问方法。
给出一个最好的例子,不是我的原创,此代码是JQuery中的源码:
// Create innerHeight, innerWidth, outerHeight and outerWidth methods
jQuery.each([ "Height", "Width" ], function( i, name ) {
	var type = name.toLowerCase();
	// innerHeight and innerWidth
	jQuery.fn["inner" + name] = function() {
		return this[0] ?
			parseFloat( jQuery.css( this[0], type, "padding" ) ) :
			null;
	};
	// outerHeight and outerWidth
	jQuery.fn["outer" + name] = function( margin ) {
		return this[0] ?
			parseFloat( jQuery.css( this[0], type, margin ? "margin" : "border" ) ) :
			null;
	};
?