Javascript 通过代理函数将参数作为第一类参数传递给 console.log
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7942323/
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
Pass arguments to console.log as first class arguments via proxy function
提问by aditya
console.log
takes an unspecified number of arguments and dumps their content in a single line.
console.log
接受未指定数量的参数并将其内容转储在一行中。
Is there a way I can write a function that passes arguments passed to it directly to console.log
to maintain that behaviour? For example:
有没有一种方法可以编写一个函数,将传递给它的参数直接传递给它console.log
以保持该行为?例如:
function log(){
if(console){
/* code here */
}
}
This would not be the same as:
这与:
function log(){
if(console){
console.log(arguments);
}
}
Since arguments
is an array and console.log
will dump the contents of that array. Nor will it be the same as:
因为arguments
是一个数组console.log
并将转储该数组的内容。它也不会与以下相同:
function log(){
if(console){
for(i=0;i<arguments.length;console.log(arguments[i]),i++);
}
}
Since that will print everything in different lines. The point is to maintain console.log
's behaviour, but through a proxy function log
.
因为这将在不同的行中打印所有内容。关键是要维护console.log
的行为,但要通过代理功能log
。
+---
+---
I was looking for a solution I could apply to all functions in the future (create a proxy for a function keeping the handling of arguments intact). If that can't be done however, I'll accept a console.log
specific answer.
我正在寻找一种可以应用于未来所有函数的解决方案(为保持参数处理完整的函数创建代理)。但是,如果不能这样做,我会接受一个console.log
具体的答案。
回答by Gabriele Petrioli
This should do it ..
这应该这样做..
function log() {
if(typeof(console) !== 'undefined') {
console.log.apply(console, arguments);
}
}
Just adding another option (using the spreadoperator and the restparameters - although arguments
could be used directly with the spread)
只需添加另一个选项(使用传播运算符和其余参数 - 尽管arguments
可以直接与传播一起使用)
function log(...args) {
if(typeof(console) !== 'undefined') {
console.log(...args);
}
}
回答by nheinrich
There is a good example from the html5boilerplate code that wraps console.log in a similar way to make sure you don't disrupt any browsers that don't recognize it. It also adds a history and smoothes out any differences in the console.log implementation.
html5boilerplate 代码中有一个很好的例子,它以类似的方式包装了 console.log,以确保您不会破坏任何无法识别它的浏览器。它还添加了历史记录并消除了 console.log 实现中的任何差异。
It was developed by Paul Irish and he has written up a post on it here.
它是由 Paul Irish 开发的,他在这里写了一篇关于它的文章。
I've pasted the relevant code below, here is a link to the file in the project: https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
我已经粘贴了下面的相关代码,这里是项目中文件的链接:https: //github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
log.history = log.history || []; // store logs to an array for reference
log.history.push(arguments);
if(this.console) {
arguments.callee = arguments.callee.caller;
var newarr = [].slice.call(arguments);
(typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
}
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}}((function(){try {console.log();return window.console;}catch(err){return window.console={};}})());
回答by Niet the Dark Absol
Yes.
是的。
console.log.apply(null,arguments);
Although, you may need to loop through the arguments object and create a regular array from it, but apart from that that's how you do it.
虽然,您可能需要遍历 arguments 对象并从中创建一个常规数组,但除此之外,您就是这样做的。