posts - 495,comments - 227,trackbacks - 0

          自己想封裝一個logger類調用console方法的時候能打出時間、模塊名稱等額外的info,而IE console下的方法不支持call、apply調用,也不能直接給console.info等方法設置call方法,很麻煩。。。
          可以利用Function.apply、Function.prototype.apply、Object.apply等方法代替:
          Function.apply.apply(consloe.info, [console, aArgs]);
          or
          Function.apply.call(consloe.info, console, aArgs);
          注:其中aArgs為數組

          function doLog(sMethod , aArgs){
                if(navigator.userAgent.toLowerCase().indexOf('msie') > -1){
                      Function.apply.apply(console[sMethod], [console, aArgs]);
                }else{
                      console[sMethod].apply(console,aArgs);
                }
          }
          var Logger=function(sModule){
                   this._name=sModule;
          };
          var methods=['log','debug','info','error','warn'];
          for(var i=0,len=methods.length; i<len; i++){
              (function(method){
                  Logger.prototype[method]=function(){
                      var sDate='['+ new Date().toLocaleString()+']';
                      var aArgs=Array.prototype.slice.call(arguments,0);
                      aArgs.unshift(sDate);
                      aArgs.push('('+ this._name+')');
                      doLog(method,aArgs);
              }})(methods[i]);
          }

          //-----------------------------
          var log=new Logger('chatList');

          log.info('hello');

          ok,成功輸出 :[2012年3月7日 18:29:23]hello(chatList)

          posted on 2013-11-18 13:42 SIMONE 閱讀(888) 評論(0)  編輯  收藏 所屬分類: JavaScript
          主站蜘蛛池模板: 桃园县| 四平市| 定西市| 社旗县| 县级市| 清河县| 清远市| 新巴尔虎右旗| 河间市| 沂源县| 门源| 卓资县| 利津县| 尉氏县| 新泰市| 丁青县| 大悟县| 阿城市| 南平市| 孙吴县| 林口县| 元江| 四会市| 厦门市| 广西| 泸定县| 丹东市| 福海县| 高唐县| 红桥区| 南华县| 塔河县| 堆龙德庆县| 遂宁市| 如皋市| 明光市| 渭源县| 长顺县| 平阴县| 大名县| 武夷山市|