javascript - 全局对象/范围和未定义的循环引用

标签 javascript node.js global circular-reference

我不确定我是否理解这几次经历的结果:

经验 n°1(在新命令行中):

> _
ReferenceError: _ is not defined
    at repl:1:2
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> global
{ global: [Circular],
  ...
 _: [Circular], }
> _
{ global: [Circular],
  ...
 _: [Circular], }

经验 n°2(在新命令行中):

> _
ReferenceError: _ is not defined
    at repl:1:2
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> var http = require('http');
undefined
> _
undefined
> global._
undefined
> global
{ global: [Circular],
  ...
 _: [Circular], }

经验 n°3(在新命令行中):

> _
ReferenceError: _ is not defined
    at repl:1:2
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> Object.prototype.toString.call(global._)
'[object Undefined]'
> _
'[object Undefined]'

这就是我到目前为止所理解的:

  1. 来自经验 n°1:

    • 当第一个 get global 被触发时,就会构建 global 对象。
    • 然后也会添加 _ 循环引用(这似乎合乎逻辑)。
  2. 经验二:

    • 访问 global 对象的属性(在本例中为 require())后,_ 被设置。
    • 由于某种原因,它已设置,但未定义不是循环引用。
  3. 来自经验 n°3:

    • 通过 global 对象 (global._) 中的属性访问 _ 并将其用作方法或对象的参数时语句,该操作的结果将 _ 的值重新分配给该结果。

这是我的问题:

  • require 等全局属性相比,为什么循环引用不能立即访问?

  • 为什么使用 global._ 的方法/语句的结果会重新分配 _

  • 访问其他全局属性(例如 require)后,为什么 _ 的值设置为 undefined 而不是 [通告]

提前谢谢您!

最佳答案

Why are results of methods / statements that use global._ reassigning _ ?

_保存最后评估语句的值:

> 'foo'
'foo'
> _
'foo'

> var http = require('http');
undefined
> _
undefined

因此,这不是 _ 的使用改变_做任何事情都会改变_ 。由于之前没有任何声明,因此最初并未声明。

After accessing other global properties like require, why is _'s value set to undefined rather than a [Circular] ?

你看到[Circular]的唯一原因在你的第一个例子中是因为 _刚刚设置为 global (因为global是最后一个评估的语句),所以global._循环引用 global 。如果您的赋值语句带有 require ,该行得出 undefined ,所以这就是放置在 _ 中的值.

关于javascript - 全局对象/范围和未定义的循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20385217/

相关文章:

c++ - 全局变量与类变量有何不同,它们是否相同?

javascript - 在 Javascript 中提取方法

javascript - javascript 中的 if else 条件无法获得正确的输出

javascript - ActionController::UnknownFormat 添加 respond_to 时

javascript - Javascript 中存在哪些类型的作用域?

node.js - NodeJS 高负载下的 NGINX 502

node.js - Ember Server : "Port 4200 is already in use", 当没有进程在 4200 上运行时

javascript - 过滤对象中的多个值

dictionary - 带 tmap 的构面图

javascript - 视觉 : How to use Per-Route Guard with Vuex getter?