I'm not sure I understand the results of these few experiences :

Experience n°1 (in new command line) :

> _
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], }

Experience n°2 (in new command line) :

> _
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], }

Experience n°3 (in new command line) :

> _
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]'

So this is what I understood until now :

  1. From experience n°1 :

    • The global object is build when the first get global is triggered.
    • The _ circular reference is then added too (which seems logical).
  2. From experience n°2 :

    • After accessing a property of the global object (require() in this case), the _ is set.
    • For some reason it is set but is undefined not a circular reference.
  3. From experience n°3 :

    • When accessing _ through it's property in the global object (global._) and using it as an argument of a method or a statement, the result of said operation reassigns the value of _ to that result.

So here are my questions :

  • Why aren't circular references accessible immediately compared to other properties of global like require ?

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

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

Thank you in advance !

有帮助吗?

解决方案

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

_ holds the value of the last evaluated statement:

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

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

Thus, it's not the use of _ that changes _, it's doing anything that changes _. It is initially undeclared because there is no previous statement.

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

The only reason you see [Circular] in your first example is because _ has just been set equal to global (because global is the last evaluated statement), so global._ circularly refers back to global. In the case of your assignment statement with require, the line yields undefined, so that's the value that gets placed in _.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top