如何安全地包装`console.log`? [英] How to safely wrap `console.log`?

查看:230
本文介绍了如何安全地包装`console.log`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想为某些合法的生产原因包括对 console.log 的一些调用,比如说像单元测试工具。显然,如果浏览器没有控制台,或者如果没有控制台

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want this to throw a premature exception if the browser doesn't have a console, or if no console is present.

什么是创建一个简单的 log 函数来记录东西到控制台的最好方法,或者如果没有控制台,默认失败,没有错误?

What's the best way to create a simple log function to log stuff to the console, or silently fail without error if no console is present?

上面链接的问题的接受答案:

The accepted answer to the question linked above:


var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);


可以记录函数在IE上正常调用,并且使用 apply 这里只是为了显示它的可能吗?并且,我认为从连接的问题,如果IE的控制台关闭时,它将失败,因此 log 即使在控制台打开后也不会工作,正确?如果这是错的,有人可以解释它的工作原理吗?

Can this log function be called normally on IE, and the use of apply here is just to show it's possible? And, I assume from the linked question that this will fail if IE's console is closed when it runs, so log won't work even after the console opens, correct? If that's wrong, can someone explain how it works?

这个 ycombinator文章似乎相关。是否正在讨论与上面链接的问题相同的IE行为?

This ycombinator article seems relevant. Are they are talking about the same IE behavior as the question linked above?


Function.prototype.apply.apply(console.log, [console, arguments]);

适用于IE9破坏的console.log和其他供应商的常规console.log。
与使用Array.prototype.slice将参数转换为实数数组相同的黑客。

Works both on IE9 broken console.log, and regular console.log from other vendors. Same hack as using Array.prototype.slice to convert arguments into a real array.

控制台。

function echo(){
  Function.prototype.apply.apply(console.log, [console, arguments]);
}

简化:

function echo(){
  Function.apply.call(console.log, console, arguments);
}

添加支票并返回:

function echo(){
  return window.console && console.log &&
         Function.apply.call(console.log, console, arguments);
}

上面的例子对我来说足够了。我没有IE手上测试它,虽然。这是安全包装 console.log 吗?的合理方法吗?

The example above looks adequate to me. I don't have IE on hand to test it, though. Is this a reasonable approach for safely wrapping console.log?

更多问题

按照以下导航回答中的链接,我们会看到以下代码:

Following the link in nav's answer below, we see the code:

Function.prototype.call.call(console.log, console,
    Array.prototype.slice.call(arguments));

arguments 转换为在这种情况下的数组?我想它必须在一些浏览器失败,如果你不这样做?而且,歌剧奇怪的行为和无控制台浏览器放在一边,应该不像这样几乎工作的每个其他浏览器以及?并且原型在上面的例子中服务一个目的,或者我们只是踩踏... Function.call.call Object.call.call 或对于这个问题 isNaN.call.call 似乎工作, code> Function.prototype.call.call 。

What is the purpose of converting arguments to an array in this case? I guess it must fail in some browser if you don't do this? And, opera weird behavior and console-less browsers aside, shouldn't something like this pretty much work for every other browser as well? And does prototype serve a purpose in the above examples, or are we just being pedantic... Function.call.call or Object.call.call or for that matter isNaN.call.call seem to work just as well as Function.prototype.call.call.

推荐答案


这个日志函数可以在IE上正常调用,可以显示吗?

Can this log function be called normally on IE, and the use of apply here is just to show it's possible?

是的,是的。


并且,我认为从连接的问题,如果IE的控制台关闭时,它将运行失败,因此日志将无法工作,即使在控制台打开后,正确吗?

And, I assume from the linked question that this will fail if IE's console is closed when it runs, so log won't work even after the console opens, correct?

正确。正如我在这个问题的答案中所说明的, console 对象在第一次为特定选项卡打开开发人员工具之前不会公开。大多数开发人员使用控制台垫片,在这种情况下 Function#bind 方法变得有点过时,因为您可以使用 Function#apply.apply

Correct. As explained in my answer on that question, the console object is not exposed until the first time the developer tools are opened for a particular tab. Most developers use a console shim, in which case the Function#bind approach becomes a little obsolete because you may as well use the Function#apply.apply method.


在这种情况下,将参数转换为数组的目的是什么?

What is the purpose of converting arguments to an array in this case?

没有一个,它是多余的。除非是自定义的日志实现,在这种情况下,开发人员可能有理由将arguments对象转换为数组。

There isn't one, it's redundant. Unless it's a custom log implementation, in which case the developer may have a reason to convert an arguments object to an array.


并且原型在上面的例子中用于一个目的,或者我们只是踩踏...

And does prototype serve a purpose in the above examples, or are we just being pedantic...

和不。一些开发人员可能不小心将 Function.call 更改为自定义函数或值。当然,他们也可以破解 Function.prototype.call ,但是这种情况不太可能发生。

Well, yes and no. Some developer may have unwittingly changed Function.call to a custom function or value. Of course, they could break Function.prototype.call too, but this is far less likely to happen by accident.

这篇关于如何安全地包装`console.log`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆