2015-05-29 64 views
2

我想重定向控制檯調用到log4javascript庫。log4javascript IE8意外錯誤「功能attendue」/「預期的功能」

因此,基本上,任何對console.log的調用都會調用log.info,log是一個Log4javascript實例。

但是當它調用log.info我得到一個「Fonction attendue」錯誤(法國),這基本上意味着「功能有望」。

我也試着撥打log.info從IE8控制檯,同樣的故事。

我不認爲這是相關的腳本,但在這裏它是區分:

(function (fallback) { 

    fallback = fallback || function() { }; 

    // function to trap most of the console functions from the FireBug Console API. 
    var trap = function() { 
     // create an Array from the arguments Object 
     var args = Array.prototype.slice.call(arguments); 
     // console.raw captures the raw args, without converting toString 
     console.raw.push(args); 
     var message = args.join(' '); 
     console.messages.push(message); 
     fallback(message); 
    }; 

    // redefine console 
    if (typeof console === 'undefined') { 
     console = { 
      messages: [], 
      raw: [], 
      dump: function() { return console.messages.join('\n'); }, 
      log: trap, 
      debug: trap, 
      info: trap, 
      warn: trap, 
      error: trap, 
      assert: trap, 
      clear: function() { 
       console.messages.length = 0; 
       console.raw.length = 0 ; 
      }, 
      dir: trap, 
      dirxml: trap, 
      trace: trap, 
      group: trap, 
      groupCollapsed: trap, 
      groupEnd: trap, 
      time: trap, 
      timeEnd: trap, 
      timeStamp: trap, 
      profile: trap, 
      profileEnd: trap, 
      count: trap, 
      exception: trap, 
      table: trap 
     }; 
    } 

})(log.info); 

我想Log4Javascript支持IE8,所以有什麼錯在這裏?謝謝。

回答

1

log4javascript確實支持IE 8的問題是this處於呼叫不正確的log.info,其預計將被稱爲一個方法,並因此不得不log參考作爲this值。我建議在記錄器對象傳遞給你的IIFE並調用其info方法修復它:

(function (log) { 
    var fallback = log ? 
      function() { 
       var args = Array.prototype.slice.call(arguments); 
       log.info.apply(log, args); 
      } : 
      function() { }; 

    // function to trap most of the console functions from the FireBug Console API. 
    var trap = function() { 
     // create an Array from the arguments Object 
     var args = Array.prototype.slice.call(arguments); 
     // console.raw captures the raw args, without converting toString 
     console.raw.push(args); 
     var message = args.join(' '); 
     console.messages.push(message); 
     fallback(message); 
    }; 

    // redefine console 
    if (typeof window.console === 'undefined') { 
     window.console = { 
      messages: [], 
      raw: [], 
      dump: function() { return console.messages.join('\n'); }, 
      log: trap, 
      debug: trap, 
      info: trap, 
      warn: trap, 
      error: trap, 
      assert: trap, 
      clear: function() { 
       console.messages.length = 0; 
       console.raw.length = 0 ; 
      }, 
      dir: trap, 
      dirxml: trap, 
      trace: trap, 
      group: trap, 
      groupCollapsed: trap, 
      groupEnd: trap, 
      time: trap, 
      timeEnd: trap, 
      timeStamp: trap, 
      profile: trap, 
      profileEnd: trap, 
      count: trap, 
      exception: trap, 
      table: trap 
     }; 
    } 
})(log); 
+0

它可能工作(現在不能測試),但我相信'apply'功能上不存在IE8 ,作爲「綁定」。 – Vadorequest

+1

@Vadorequest:'應用程序'肯定存在於IE 8中。我認爲它自5.5版起就已經在IE中。不過,你說'綁定'是正確的,那很晚。在標準方面,'apply'是ECMAScript 3,'bind'是ECMAScript 5. –

+0

很高興當時錯了:)我會盡快測試這個。 – Vadorequest