NOTE
Bun 提供了一个浏览器和 Node.js 兼容的 [console](https://developer.mozilla.org/en-US/docs/Web/API/console) 全局对象。本页面仅记录 Bun 原生的 API。对象检查深度
Bun 允许你配置 console.log() 输出中嵌套对象的显示深度:
- CLI 标志:使用
--console-depth <number>为单次运行设置深度 - 配置:在
bunfig.toml中设置console.depth进行持久配置 - 默认:对象检查深度为
2层
js
const nested = { a: { b: { c: { d: "deep" } } } };
console.log(nested);
// 默认(深度 2):{ a: { b: [Object] } }
// 深度 4:{ a: { b: { c: { d: 'deep' } } } }CLI 标志优先于配置文件设置。
从 stdin 读取
在 Bun 中,console 对象可以用作 AsyncIterable 来顺序读取 process.stdin 的行。
ts
for await (const line of console) {
console.log(line);
}这对于实现交互式程序很有用,如以下加法计算器。
ts
console.log(`让我们添加一些数字!`);
console.write(`计数:0\n> `);
let count = 0;
for await (const line of console) {
count += Number(line);
console.write(`计数:${count}\n> `);
}要运行此文件:
bash
bun adder.ts
让我们添加一些数字!
计数:0
> 5
计数:5
> 5
计数:10
> 5
计数:15