node/doc/api/globals.markdown

134 lines
3.0 KiB
Markdown
Raw Normal View History

2012-02-28 03:09:33 +08:00
# Global Objects
<!-- type=misc -->
2010-10-28 20:18:16 +08:00
These objects are available in all modules. Some of these objects aren't
actually in the global scope but in the module scope - this will be noted.
2010-10-28 20:18:16 +08:00
2012-02-28 03:09:33 +08:00
## global
<!-- type=global -->
2010-10-28 20:18:16 +08:00
2012-02-28 03:09:33 +08:00
* {Object} The global namespace object.
2010-10-28 20:18:16 +08:00
2011-01-18 05:34:22 +08:00
In browsers, the top-level scope is the global scope. That means that in
browsers if you're in the global scope `var something` will define a global
variable. In Node this is different. The top-level scope is not the global
scope; `var something` inside a Node module will be local to that module.
2012-02-28 03:09:33 +08:00
## process
<!-- type=global -->
* {Object}
2010-10-28 20:18:16 +08:00
The process object. See the [process object](process.html#process) section.
2010-10-28 20:18:16 +08:00
2012-02-28 03:09:33 +08:00
## console
<!-- type=global -->
* {Object}
2011-04-19 07:52:53 +08:00
Used to print to stdout and stderr. See the [stdio](stdio.html) section.
2012-02-28 03:09:33 +08:00
## Buffer
<!-- type=global -->
* {Object}
Used to handle binary data. See the [buffers](buffers.html) section.
2011-04-19 07:52:53 +08:00
2012-02-28 03:09:33 +08:00
## require()
<!-- type=var -->
* {Function}
2010-10-28 20:18:16 +08:00
To require modules. See the [Modules](modules.html#modules) section.
`require` isn't actually a global but rather local to each module.
2010-10-28 20:18:16 +08:00
### require.resolve()
Use the internal `require()` machinery to look up the location of a module,
but rather than loading the module, just return the resolved filename.
2011-06-03 14:14:35 +08:00
### require.cache
2012-02-28 03:09:33 +08:00
* {Object}
2011-06-03 14:14:35 +08:00
Modules are cached in this object when they are required. By deleting a key
value from this object, the next `require` will reload the module.
2012-02-28 03:09:33 +08:00
## __filename
<!-- type=var -->
2011-06-03 14:14:35 +08:00
2012-02-28 03:09:33 +08:00
* {String}
2010-10-28 20:18:16 +08:00
The filename of the code being executed. This is the resolved absolute path
of this code file. For a main program this is not necessarily the same
filename used in the command line. The value inside a module is the path
to that module file.
2010-10-28 20:18:16 +08:00
Example: running `node example.js` from `/Users/mjr`
console.log(__filename);
// /Users/mjr/example.js
`__filename` isn't actually a global but rather local to each module.
2012-02-28 03:09:33 +08:00
## __dirname
<!-- type=var -->
* {String}
2010-10-28 20:18:16 +08:00
The name of the directory that the currently executing script resides in.
2010-10-28 20:18:16 +08:00
Example: running `node example.js` from `/Users/mjr`
console.log(__dirname);
// /Users/mjr
`__dirname` isn't actually a global but rather local to each module.
2010-10-28 20:18:16 +08:00
2012-02-28 03:09:33 +08:00
## module
<!-- type=var -->
* {Object}
2010-10-28 20:18:16 +08:00
2010-11-24 03:14:20 +08:00
A reference to the current module. In particular
`module.exports` is the same as the `exports` object. See `src/node.js`
2010-10-28 20:18:16 +08:00
for more information.
`module` isn't actually a global but rather local to each module.
2012-02-28 03:09:33 +08:00
## exports
<!-- type=var -->
An object which is shared between all instances of the current module and
made accessible through `require()`.
`exports` is the same as the `module.exports` object. See `src/node.js`
for more information.
`exports` isn't actually a global but rather local to each module.
2012-02-28 03:09:33 +08:00
See the [module system documentation](modules.html) for more
information.
See the [module section](modules.html) for more information.
## setTimeout(cb, ms)
## clearTimeout(t)
## setInterval(cb, ms)
## clearInterval(t)
<!--type=global-->
The timer functions are global variables. See the [timers](timers.html) section.