Ich hatte eine Menge Probleme mit Entwicklern, die ihre console.() Anweisungen eincheckten. Und Ich mag nicht Internet Explorer debuggen trotz der fantastischen Verbesserungen von Internet Explorer 10 und Visual Studio 2012 usw.
Also, ich habe außer Kraft gesetzt die Konsole Objekt selbst ... Ich habe eine __localhost Flagge hinzugefügt, dass nur Erlaubt Konsolenkonten wenn auf localhost. Ich fügte auch Konsole hinzu.() Funktioniert Internet Explorer (der stattdessen eine Warnung() anzeigt).
// Console extensions...
(function() {
var __localhost = (document.location.host === "localhost"),
__allow_examine = true;
if (!console) {
console = {};
}
console.__log = console.log;
console.log = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__log === "function") {
console.__log(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert(msg);
}
}
};
console.__info = console.info;
console.info = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__info === "function") {
console.__info(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert(msg);
}
}
};
console.__warn = console.warn;
console.warn = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__warn === "function") {
console.__warn(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert(msg);
}
}
};
console.__error = console.error;
console.error = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__error === "function") {
console.__error(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert(msg);
}
}
};
console.__group = console.group;
console.group = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__group === "function") {
console.__group(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert("group:\r\n" + msg + "{");
}
}
};
console.__groupEnd = console.groupEnd;
console.groupEnd = function() {
if (__localhost) {
if (typeof console !== "undefined" && typeof console.__groupEnd === "function") {
console.__groupEnd(arguments);
} else {
var i, msg = "";
for (i = 0; i < arguments.length; ++i) {
msg += arguments[i] + "\r\n";
}
alert(msg + "\r\n}");
}
}
};
/// <summary>
/// Clever way to leave hundreds of debug output messages in the code,
/// but not see _everything_ when you only want to see _some_ of the
/// debugging messages.
/// </summary>
/// <remarks>
/// To enable __examine_() statements for sections/groups of code, type the
/// following in your browser's console:
/// top.__examine_ABC = true;
/// This will enable only the console.examine("ABC", ...) statements
/// in the code.
/// </remarks>
console.examine = function() {
if (!__allow_examine) {
return;
}
if (arguments.length > 0) {
var obj = top["__examine_" + arguments[0]];
if (obj && obj === true) {
console.log(arguments.splice(0, 1));
}
}
};
})();
Beispiel für die Verwendung:
console.log("hello");
Chrome/Firefox:
prints hello in the console window.
Internet Explorer:
displays an alert with 'hello'.
Für diejenigen, die eng auf den Code anschauen, werden Sie Entdecken Sie die Funktion console.examine(). Ich habe dies vor Jahren erstellt, damit ich Debug-Code in bestimmten Bereichen um das Produkt lassen kann, um die Fehlerbehebung bei QA/Kundenproblemen zu erleichtern. Zum Beispiel würde ich die folgende Zeile in einigem freigegebenen Code verlassen:
function doSomething(arg1) {
// ...
console.examine("someLabel", arg1);
// ...
}
Und dann aus dem freigesetzten Produkt, geben Sie Folgendes in die Konsole (oder Adressleiste mit dem Präfix ‚javascript:‘):
top.__examine_someLabel = true;
Dann werde ich alle protokollierte console.examine() -Anweisungen sehen. Es war schon immer eine fantastische Hilfe.
gerade realisiert, 'console.log()' ist genial für js Debuggen ... Ich vergesse oft es in der Praxis verwendet wird. – Ish
Wie lange kann einer dieser "Ausgänge" sein? Upvote übrigens, das war wirklich hilfreich – nbura
im Jahr 2013 sollte niemand diese LOL tun :) – dbrin