redsos

导航

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}

posted on 2007-11-10 15:00  redsos  阅读(392)  评论(0编辑  收藏  举报