Why are functions not available in the global object?

This script has a different behavior based on whether it is run from the node js shell or stored in a script file passed to node.

Script:

var a = 1;
function b(){return 1;}
for(var k in global) console.log(k);

Output in the shell (only the last 4 lines correspond to IMO). Each of the three lines was sequentially inserted / inserted into the node REPL instance running in the terminal on Mac OS X):

ArrayBuffer
Int8Array
Uint8Array
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
DataView
global
process
GLOBAL
root
Buffer
setTimeout
setInterval
clearTimeout
clearInterval
console
module
require
a
_
b
k

Output at startup as a saved script (called node myscript.jsfrom bash on Mac OS X):

ArrayBuffer
Int8Array
Uint8Array
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
DataView
global
process
GLOBAL
root
Buffer
setTimeout
setInterval
clearTimeout
clearInterval
console

Why are they different, and why my script cannot see aand bin global?

EDIT: adding an extra c = 2 operator changed the output. c was visible in both script startup methods. This still does not explain the presence of a and b when running the script from the shell.

+5
1

, Node REPL "" "this" ( global === this).

. , , - :

function (module, exports, global) {
  // your module code
}

, var script, . REPL var .

+4

All Articles