JavaScript 中标准化自定义异常信息。
sample:
1String.prototype.toChinese = function(){
2 if(isNaN(this)) throw({description:"非法数字!",number:-1,toString:function(){ return this.description}});
3 var chinese = "零壹贰叁肆伍陆柒捌玖";
4 return this.replace(/[0-9]/g,function($1){
5 return chinese.charAt($1);
6 });
7}
8
9try{
10 alert("123456789s1234".toChinese());
11}catch(e){
12 alert("e: " + e + "\ndescription: " + e.description + "\nnumber: " + e.number);
13}
2 if(isNaN(this)) throw({description:"非法数字!",number:-1,toString:function(){ return this.description}});
3 var chinese = "零壹贰叁肆伍陆柒捌玖";
4 return this.replace(/[0-9]/g,function($1){
5 return chinese.charAt($1);
6 });
7}
8
9try{
10 alert("123456789s1234".toChinese());
11}catch(e){
12 alert("e: " + e + "\ndescription: " + e.description + "\nnumber: " + e.number);
13}