VS 代码 & Node.js:使用 console() 而不是 console.log()?
我正在编写要在专业地理空间软件中使用的JavaScript表达式。
在地理空间软件中,只有 console()
有效,而 console.log()
无效。
当我工作时,我正在 使用 Node.JS 的 VS 代码 (因为地理空间软件没有 linting 功能等)。
据我了解,Node.js 使用 console.log()
语法,而不是 console()
。
因此,当在两个程序之间来回移动脚本时,我需要执行查找和替换以从 console()
切换到 console.log( )
(反之亦然)。这变得很乏味。
问题:
在 VS Code / Node.js 中,有没有办法使用 console()
而不是 console.log()
?这将使开发脚本时的事情变得更加容易。我将能够拥有一个适用于这两个程序的标准脚本。
我是一个新手,所以外行人的术语将不胜感激。
I'm writing JavaScript expressions to be used in specialized geospatial software.
In the geospatial software, only console()
is valid, not console.log()
.
As I'm working, I'm debugging my geospatial scripts in VS Code using Node.JS (since the geospatial software doesn't have linting functionality, etc.).
It's my understanding that Node.js uses console.log()
syntax, not console()
.
Therefore, when moving the script back and forth between the two programs, I need to do a find and replace to switch from console()
to console.log()
(and vice versa). This is becoming tedious.
Question:
In VS Code / Node.js, is there a way to use console()
instead of console.log()
? That would make things a lot easier when developing my scripts. I'd be able to have a standard script that works in both programs.
I'm a novice, so layman's terms would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想不出一种方法可以做到这一点,而不会潜在地破坏您可能正在使用的其他模块中的代码,因为它需要重新分配全局的
console
,那是……全球性的。 (我在下面列出了一个可能的选项,但是......我不喜欢它。)但即使你能找到一种方法来做到这一点,我也不会。相反,在脚本的顶部,我会添加类似以下内容的内容:
...然后在整个脚本中使用
log
(或您想要使用的任何名称)。它将引用适当的函数。这是可能不会破坏其他代码的东西:
用
console.log
替换console
,但也将所有console< /code> 属性(好吧,无论如何,自己的可枚举属性)在函数上作为属性,因此
console
和console.log
(以及console.error等)工作。但不能保证毫无戒心的模块中的代码不会执行
if (typeof console === "object")
来决定是否可以使用console
,这会失败,因为typeof
会返回"function"
。再说一遍:我将使用上面的第一个解决方案:独立的
log
函数。I can't think of a way to do that that wouldn't potentially blow up code in other modules that you may be using, since it would require reassigning the
console
global, and that's...global. (I have included one possible option below, but...I don't like it.)But even if you can find a way to do that, I wouldn't. Instead, at the top of the script, I'd have something like:
...and then use
log
(or whatever name you want to use) throughout the script. It'll refer to the appropriate function.Here's something that might not blow up other code:
That replaces
console
withconsole.log
, but also puts all of theconsole
properties (well, the own enumerable ones anyway) on the function as properties, so bothconsole
andconsole.log
(andconsole.error
, etc.) work. But there's no guarantee that code in an unsuspecting module doesn't doif (typeof console === "object")
to decide whether it can useconsole
, and that would fail becausetypeof
would return"function"
for it instead.So again: I'd use the first solution above: A standalone
log
function.