gpt4 book ai didi

javascript - 打印函数调用者行号的更好方法

转载 作者:行者123 更新时间:2023-11-30 14:16:40 25 4
gpt4 key购买 nike

所以我有一个定义如下的函数:

printPrettyJson =
function (json)
{
// logger.log(chalk.magenta(JSON.stringify(json, null, 4)))
try
{
throw Error("printPrettyJson")
}
catch (error)
{
console.log(error.stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
}

我用它做的是简单地打印出一种 JSON 类型的对象(因为否则它会显示 [Object object]。但是,我想知道我是否在不抛出笨拙的情况下这样做错误。我正在从另一个文件调用它,例如

文件1

const printPrettyJson = require("file2)

Line 101: printPrettyJson(json)

文件2

// printPrettyJson's definition in here

那么如何在 printPrettyJson 的函数调用中从 File1 输出 line 101

最佳答案

您可以使用 console.trace 输出调用堆栈而不引发错误。 .它在 standardconsole API 规范。

关于javascript - 打印函数调用者行号的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53475791/

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