我不确定我是否理解这几次经历的结果:
经验 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]'
这就是我到目前为止所理解的:
来自经验 n°1:
- 当第一个
get global
被触发时,就会构建global
对象。 - 然后也会添加
_
循环引用(这似乎合乎逻辑)。
- 当第一个
经验二:
- 访问
global
对象的属性(在本例中为require()
)后,_
被设置。 - 由于某种原因,它已设置,但
未定义
不是循环引用。
- 访问
来自经验 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/