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 :
From experience n°1 :
- The
globalobject is build when the firstget globalis triggered. - The
_circular reference is then added too (which seems logical).
- The
From experience n°2 :
- After accessing a property of the
globalobject (require()in this case), the_is set. - For some reason it is set but is
undefinednot a circular reference.
- After accessing a property of the
From experience n°3 :
- When accessing
_through it's property in theglobalobject (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.
- When accessing
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 toundefinedrather than a[Circular]?
Thank you in advance !
_holds the value of the last evaluated statement: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.The only reason you see
[Circular]in your first example is because_has just been set equal toglobal(becauseglobalis the last evaluated statement), soglobal._circularly refers back toglobal. In the case of your assignment statement withrequire, the line yieldsundefined, so that's the value that gets placed in_.