【问题标题】:In Node.js, on the console object, what is the context function?在 Node.js 中,在控制台对象上,上下文函数是什么?
【发布时间】: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],

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

[Symbol(counts)] 对象可能与 countcountReset 的实现。

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

【问题讨论】:

    标签: node.js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      相关资源
      最近更新 更多