javascript 将多个参数传递给 console.log
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18746440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing multiple arguments to console.log
提问by And Finally
I have a utility function that wraps console.log with a condition, so we only call console.log if we're in the dev environment and console.log exists:
我有一个用条件包装 console.log 的实用函数,所以我们只在开发环境中调用 console.log 并且 console.log 存在:
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(message);
}
};
}());
This has worked very well for normal console logs. But I've recently discovered the joys of passing more than one argument to console.log: it allows you to prefix a console log with a string, so console.log('DEBUG', object)outputs the string plus an expandable object whose properties you can inspect. How can I change my conlog function to do this? I've tried logging out all arguments like this:
这对于普通的控制台日志非常有效。但我最近发现了向 console.log 传递多个参数的乐趣:它允许您在控制台日志前加上一个字符串,因此console.log('DEBUG', object)输出字符串加上一个您可以检查其属性的可扩展对象。如何更改我的 conlog 功能来执行此操作?我试过像这样注销所有参数:
metro.conlog = (function () {
return function (message) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(arguments);
}
};
}());
But this outputs the arguments as an array, instead of the neat line you get with console.log. You can see the difference in this screenshot:
但这会将参数作为数组输出,而不是使用 console.log 获得的简洁线条。您可以在此屏幕截图中看到不同之处:


Can anybody tell me how I can reproduce the original log output?
谁能告诉我如何重现原始日志输出?
回答by Idham Perdameian
Of course you can do it, thisis a demo of how to do exactly what you need, with extra options added.
当然你可以做到,这是一个演示如何准确地做你需要的,添加了额外的选项。
And the code is below:
代码如下:
var mylog = (function () {
return {
log: function() {
var args = Array.prototype.slice.call(arguments);
console.log.apply(console, args);
},
warn: function() {
var args = Array.prototype.slice.call(arguments);
console.warn.apply(console, args);
},
error: function() {
var args = Array.prototype.slice.call(arguments);
console.error.apply(console, args);
}
}
}());
var name = "Alex";
var arr = [1, 2, 3];
var obj = { a:1, b:2, c:3 };
var hello = function(msg){alert(msg);};
mylog.log("Name: ", name);
mylog.log("Window Debug: ", window);
mylog.error("Some error happened");
mylog.warn("Ahh... Warning", arr, obj);
mylog.log("more parameters: ", arr, obj, hello);
回答by Justin Wood
Try something like this
尝试这样的事情
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message, object) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(message, object);
}
};
}());
where messageis something like "DEBUG" and objectis whatever object you want to examine.
哪里message是“DEBUG”之类的东西,object是您要检查的任何对象。
If you want to be able to pass an arbitrary number of arguments into console.log, I would suggest using the argumentsvariable.
如果您希望能够将任意数量的参数传递给console.log,我建议使用该arguments变量。
/* Console log if environment has debug true or #debug initially passed in URL */
metro.conlog = (function () {
return function (message, object) {
if ((metro.siteData.debug || metro.hashOptions.hasOwnProperty('debug')) && window.console && message) {
console.log(arguments);
}
};
}());
As mentioned in my comments, I am unsure which browsers fully support this (I'm looking at you IE).
正如我在评论中提到的,我不确定哪些浏览器完全支持这一点(我在看你的 IE)。
I have tested and confirmed that it works in current Chrome, FireFox and Safari.
我已经测试并确认它适用于当前的 Chrome、FireFox 和 Safari。

