它是在beta中,但不是在发布中?
对于后备甚至更好的是:
var alertFallback = true; if (typeof console === "undefined" || typeof console.log === "undefined") { console = {}; if (alertFallback) { console.log = function(msg) { alert(msg); }; } else { console.log = function() {}; } }
console.log仅在打开开发人员工具(F12可以将其切换为打开和关闭)后可用。有趣的是,在打开它之后,你可以将其关闭,然后仍然通过console.log调用发布到它,并且在你重新打开它时会看到它们。我认为这是种错误,可能已修复,但我们会看到。
我可能只会使用以下内容:
function trace(s) { if ('console' in self && 'log' in console) console.log(s) // the line below you might want to comment out, so it dies silent // but nice for seeing when the console is available or not. else alert(s) }
甚至更简单:
function trace(s) { try { console.log(s) } catch (e) { alert(s) } }