今天在forum中看到一个很有趣的帖子,就是在javascript中实现方法的重载了,如果有兴趣的话去看看Method Overloading with JavaScript吧;本想改简单点再贴上来的,不过还是想想还是算了,原来的例子也不是很复杂。
//
Création d'un namespace
Type.registerNamespace( ' Sample ' );
// Constructeur de la class Personne
Sample.Person = function (firstName, lastName){
this ._firstName = firstName;
this ._lastName = lastName;
}
// Rajout des différentes méthodes à notre type
Sample.Person.prototype = {
get_firstName : function (){
return this ._firstName;
},
set_firstName : function (value){
this ._firstName = value;
},
get_lastName : function (){
return this ._lastName;
},
set_lastName : function (value){
this ._lastName = value;
},
toString : function (){
if (arguments.length == 1 && Object.getType(arguments[0]) == String){
return this.toString$String.apply(this , arguments);
} else if (arguments.length == 2 && Object.getType(arguments[0]) == String && Object.getType(arguments[1]) == Sample.Person){
return this.toString$String$Person.apply(this , arguments);
} else {
return this.toString$.apply(this , arguments);
}
},
toString$ : function (){
return String.format( ' je suis {0} {1} ' , this .get_firstName(), this .get_lastName());
},
toString$String : function (format){
return String.format(format, this .get_firstName(), this .get_lastName());
},
toString$String$Person : function (format, person){
return String.format( ' {0} , {1} ' , this .toString(format), person.toString(format));
}
}
// Enregistrement de notre type dans le framework Atlas
Sample.Person.registerClass( ' Sample.Person ' );
if(Sys && Sys.Application){Sys.Application.notifyScriptLoaded();}
Type.registerNamespace( ' Sample ' );
// Constructeur de la class Personne
Sample.Person = function (firstName, lastName){
this ._firstName = firstName;
this ._lastName = lastName;
}
// Rajout des différentes méthodes à notre type
Sample.Person.prototype = {
get_firstName : function (){
return this ._firstName;
},
set_firstName : function (value){
this ._firstName = value;
},
get_lastName : function (){
return this ._lastName;
},
set_lastName : function (value){
this ._lastName = value;
},
toString : function (){
if (arguments.length == 1 && Object.getType(arguments[0]) == String){
return this.toString$String.apply(this , arguments);
} else if (arguments.length == 2 && Object.getType(arguments[0]) == String && Object.getType(arguments[1]) == Sample.Person){
return this.toString$String$Person.apply(this , arguments);
} else {
return this.toString$.apply(this , arguments);
}
},
toString$ : function (){
return String.format( ' je suis {0} {1} ' , this .get_firstName(), this .get_lastName());
},
toString$String : function (format){
return String.format(format, this .get_firstName(), this .get_lastName());
},
toString$String$Person : function (format, person){
return String.format( ' {0} , {1} ' , this .toString(format), person.toString(format));
}
}
// Enregistrement de notre type dans le framework Atlas
Sample.Person.registerClass( ' Sample.Person ' );
if(Sys && Sys.Application){Sys.Application.notifyScriptLoaded();}
这里的方法的重载 重点就在于arguments这个变量,根据它的length和type判断要调用的方法,当然,每个函数所自带apply方法也功不可没噢
最后,在 .aspx 页面里再加入下面javascript就可以看到效果了。
window.pageLoad
=
function
(){
var p = new Sample.Person( ' Cyril ' , ' Durand ' );
alert(p.toString());
alert(p.toString( ' {0} {1} ' ));
var p2 = new Sample.Person( ' Toto ' , ' Bidule ' );
alert(p.toString( ' {0} {1} ' , p2));
}
var p = new Sample.Person( ' Cyril ' , ' Durand ' );
alert(p.toString());
alert(p.toString( ' {0} {1} ' ));
var p2 = new Sample.Person( ' Toto ' , ' Bidule ' );
alert(p.toString( ' {0} {1} ' , p2));
}