在Node.js中,在控制台对象上,上下文函数是什么?

时间:2018-05-28 20:05:05

标签: node.js

查看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],

构造函数ConsolecountReset记录在 https://nodejs.org/api/console.html

[Symbol(counts)]对象可能与该对象有关 实施countcountReset

context似乎应该记录在案,而不是。什么是 它?

1 个答案:

答案 0 :(得分:2)

console.contextexperimental Chrome/V8 feature,为控制台输出创建新的上下文。它没有Node-specific implementation,所以它应该在V8中实现。

由于它是实验性的,可能存在不一致之处。 console.context在最近的Chromium版本中没有像我预期的那样工作,并且它不应该在Node.js中提供有用的结果。