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