【发布时间】:2018-05-28 20:05:05
【问题描述】:
查看 Node.js repl 中的 console 对象,我发现它有一个 context 函数:
> console
Console {
log: [Function: bound consoleCall],
debug: [Function: bound consoleCall],
info: [Function: bound consoleCall],
warn: [Function: bound consoleCall],
error: [Function: bound consoleCall],
dir: [Function: bound consoleCall],
time: [Function: bound consoleCall],
timeEnd: [Function: bound consoleCall],
trace: [Function: bound consoleCall],
assert: [Function: bound consoleCall],
clear: [Function: bound consoleCall],
count: [Function: bound consoleCall],
countReset: [Function: bound countReset],
group: [Function: bound consoleCall],
groupCollapsed: [Function: bound consoleCall],
groupEnd: [Function: bound consoleCall],
Console: [Function: Console],
dirxml: [Function: dirxml],
table: [Function: table],
markTimeline: [Function: markTimeline],
profile: [Function: profile],
profileEnd: [Function: profileEnd],
timeline: [Function: timeline],
timelineEnd: [Function: timelineEnd],
timeStamp: [Function: timeStamp],
context: [Function: context],
[Symbol(counts)]: Map {} }
该函数在调用时似乎返回了一个类似于console 的对象,但缺少一些东西:
> console.context()
{ debug: [Function: debug],
error: [Function: error],
info: [Function: info],
log: [Function: log],
warn: [Function: warn],
dir: [Function: dir],
dirXml: [Function: dirXml],
table: [Function: table],
trace: [Function: trace],
group: [Function: group],
groupCollapsed: [Function: groupCollapsed],
groupEnd: [Function: groupEnd],
clear: [Function: clear],
count: [Function: count],
assert: [Function: assert],
markTimeline: [Function: markTimeline],
profile: [Function: profile],
profileEnd: [Function: profileEnd],
timeline: [Function: timeline],
timelineEnd: [Function: timelineEnd],
time: [Function: time],
timeEnd: [Function: timeEnd],
timeStamp: [Function: timeStamp] }
> console.context() === console
false
虽然看起来很相似,但功能却不尽相同:
> console.context().log('hello world')
undefined
这些项目在console 中,但不在console.context() 中:
Console: [Function: Console],
[Symbol(counts)]: Map {}
context: [Function: context],
countReset: [Function: bound countReset],
构造函数 Console 和 countReset 记录在
https://nodejs.org/api/console.html
[Symbol(counts)] 对象可能与
count 和 countReset 的实现。
但是context 似乎应该记录在案,而不是。什么是
吗?
【问题讨论】:
标签: node.js