查看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
似乎应该记录在案,而不是。什么是
它?
答案 0 :(得分:2)
console.context
是experimental Chrome/V8 feature,为控制台输出创建新的上下文。它没有Node-specific implementation,所以它应该在V8中实现。
由于它是实验性的,可能存在不一致之处。 console.context
在最近的Chromium版本中没有像我预期的那样工作,并且它不应该在Node.js中提供有用的结果。