mootools 学习 overloadSetter

Function.prototype.overloadSetter = function(usePlural){
	var self = this;
	return function(a, b){
		if(a == null) return this;
		if(usePlural || typeof a != 'string'){
			for(var k in a) self.call(this, k, a[k]);
			if(enumerables) for (var i = enumerables.length; i--;){
				k = enumerables[i];
				if(a.hasOwnProperty(k)) self.call(this, k, a[k]);
			}
		}
		else{
			self.call(this, a, b);
		}
		return this;
	};
};

进行function的重载,在进行function.overloadSetter中参数传入为true或者参数是对象不是string时直接进行方法参数的转换,如('a', 'b')->({'a':'b'}),如果要对该方法进行不同参数的调用时可以传入({'a':'b', 'a':'b'})那么的话该方法就会被根据参数执行两次。
ep:
  var test = function(a, b){
    return (a + "!!!" + b);
  }
  test.overloadSetter();
  test('a', 'b'); //a!!!b
  test({'a':'b'}); //a!!!b
  test({'a':'b', 'b':'c'}) //a!!!b b!!!c


你可能感兴趣的:(C++,c,prototype,C#,mootools)