gpt4 book ai didi

node.js - Node vs Chrome,将 console.log 分配给变量?

转载 作者:搜寻专家 更新时间:2023-10-31 22:19:26 24 4
gpt4 key购买 nike

当我将 console.log 分配给 node.js 中的一个变量时,它工作正常,

var l = console.log
l(1) # outputs 1

但是,如果我在 Chromium 30 的开发工具中做同样的事情,

var l = console.log
l(1) # TypeError: Illegal invocation

为什么它在 Chromium 的开发工具中不起作用?为什么我得到,

TypeError: Illegal invocation

最佳答案

确切地 为什么 有这个要求,我不知道,但我猜 Chrome 的 console.log 需要 this 的值成为控制台。如果您想将其存储在变量中,则必须绑定(bind) this 的值:

var l = console.log.bind(console);

关于node.js - Node vs Chrome,将 console.log 分配给变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19887424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com