节点与Chrome,将console.log分配给一个变量? [英] Node vs Chrome, assigning console.log to a variable?
本文介绍了节点与Chrome,将console.log分配给一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我将 console.log
分配给node.js中的一个变量时,它工作正常,
var l = console.log
l(1)#outputs 1
<但是,如果我在Chromium 30的开发工具中做同样的事情,
var l = console.log
l (1)#TypeError:非法调用
在Chromium的开发工具中它怎么不起作用?为什么我得到
TypeError:非法调用
这个要求已经到位,我不知道,但我猜Chrome的
console.log code>要求这个
的值为 console
。如果你想把它存储在一个变量中,你必须绑定这个
的值: var l = console.log.bind(console);
When I assign console.log
to a variable in node.js it works fine,
var l = console.log
l(1) # outputs 1
However, if I do the same thing in Chromium 30's dev tools,
var l = console.log
l(1) # TypeError: Illegal invocation
How come it doesn't work in Chromium's dev tools? Why am I getting,
TypeError: Illegal invocation
解决方案 Exactly why this requirement is in place, I don't know, but I guess Chrome's console.log
requires the value of this
to be console
. If you want to store it in a variable, you'll have to bind the value of this
:
var l = console.log.bind(console);
这篇关于节点与Chrome,将console.log分配给一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文