知识共享许可协议
本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可。

Node.js v0.10.18 手册 & 文档


全局对象#

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.

这些对象在所有模块中都是可用的。有些对象实际上并非在全局作用域内而是在模块作用域内——这种情况在以下文档中会特别指出。

global#

  • {Object} The global namespace object.

  • {Object} 全局命名空间对象。

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.

在浏览器中,顶级作用域就是全局作用域。这就是说,在浏览器中,如果当前是在全局作用域内,var something将会声明一个全局变量。在Node中则不同。顶级作用域并非全局作用域,在Node模块里的var something只属于那个模块。

process#

  • {Object}

  • {Object}

The process object. See the process object section.

进程对象。见 进程对象章节。

console#

  • {Object}

  • {Object}

Used to print to stdout and stderr. See the console section.

用于打印标准输出和标准错误。见控制台章节。

类: Buffer#

  • {Function}

  • {Function}

Used to handle binary data. See the buffer section

用于处理二进制数据。见Buffer章节。

require()#

  • {Function}

  • {Function}

To require modules. See the Modules section. require isn't actually a global but rather local to each module.

引入模块。见Modules章节。require实际上并非全局的而是各个模块本地的。

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.

使用内部的require()机制查找模块的位置,但不加载模块,只返回解析过的模块文件路径。

require.cache#

  • Object

  • Object

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.

模块在引入时会缓存到该对象。通过删除该对象的键值,下次调用require时会重新加载相应模块。

require.extensions#

稳定度:0 - 已废弃
  • {Object}

  • {Object}

Instruct require on how to handle certain file extensions.

指导require方法如何处理特定的文件扩展名。

Process files with the extension .sjs as .js:

.sjs文件作为.js文件处理:

require.extensions['.sjs'] = require.extensions['.js'];

Deprecated In the past, this list has been used to load non-JavaScript modules into Node by compiling them on-demand. However, in practice, there are much better ways to do this, such as loading modules via some other Node program, or compiling them to JavaScript ahead of time.

已废弃 之前,该列表用于按需编译非JavaScript模块并加载进Node。然而,实践中有更好的方式实现该功能,如通过其他Node程序加载模块,或提前将他们编译成JavaScript代码。

Since the Module system is locked, this feature will probably never go away. However, it may have subtle bugs and complexities that are best left untouched.

由于模块系统的API已锁定,该功能可能永远不会去掉。改动它可能会产生细微的错误和复杂性,所以最好保持不变。

__filename#

  • {String}

  • {String}

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.

当前所执行代码文件的文件路径。这是该代码文件经过解析后的绝对路径。对于主程序来说,这和命令行中使用的文件路径未必是相同的。在模块中此变量值是该模块文件的路径。

Example: running node example.js from /Users/mjr

例子:在/Users/mjr下运行node example.js

console.log(__filename);
// /Users/mjr/example.js

__filename isn't actually a global but rather local to each module.

__filename实际上并非全局的而是各个模块本地的。

__dirname#

  • {String}

  • {String}

The name of the directory that the currently executing script resides in.

当前执行脚本所在目录的目录名。

Example: running node example.js from /Users/mjr

例子:在/Users/mjr下运行node example.js

console.log(__dirname);
// /Users/mjr

__dirname isn't actually a global but rather local to each module.

__dirname实际上并非全局的而是各个模块本地的。

module#

  • {Object}

  • {Object}

A reference to the current module. In particular module.exports is the same as the exports object. module isn't actually a global but rather local to each module.

当前模块的引用。特别地,module.exportsexports指向同一个对象。module实际上并非全局的而是各个模块本地的。

See the module system documentation for more information.

详情可见模块系统文档

exports#

A reference to the module.exports object which is shared between all instances of the current module and made accessible through require(). See module system documentation for details on when to use exports and when to use module.exports. exports isn't actually a global but rather local to each module.

module.exports对象的引用,该对象被当前模块的所有实例所共享,通过require()可访问该对象。 何时使用exports以及何时使用module.exports的详情可参见模块系统文档exports实际上并非全局的而是各个模块本地的。

See the module system documentation for more information.

详情可见模块系统文档

See the module section for more information.

关于模块系统的更多信息可参见模块

setTimeout(cb, ms)#

Run callback cb after at least ms milliseconds. The actual delay depends on external factors like OS timer granularity and system load.

至少ms毫秒后调用回调cb。实际延迟取决于外部因素,如操作系统定时器粒度及系统负载。

The timeout must be in the range of 1-2,147,483,647 inclusive. If the value is outside that range, it's changed to 1 millisecond. Broadly speaking, a timer cannot span more than 24.8 days.

超时值必须在1-2147483647的范围内(包含1和2147483647)。如果该值超出范围,则该值被当作1毫秒处理。一般来说,一个定时器不能超过24.8天。

Returns an opaque value that represents the timer.

返回一个代表该定时器的句柄值。

clearTimeout(t)#

Stop a timer that was previously created with setTimeout(). The callback will not execute.

停止一个之前通过setTimeout()创建的定时器。回调不会再被执行。

setInterval(cb, ms)#

Run callback cb repeatedly every ms milliseconds. Note that the actual interval may vary, depending on external factors like OS timer granularity and system load. It's never less than ms but it may be longer.

每隔ms毫秒重复调用回调cb。注意,取决于外部因素,如操作系统定时器粒度及系统负载,实际间隔可能会改变。它不会少于ms但可能比ms长。

The interval must be in the range of 1-2,147,483,647 inclusive. If the value is outside that range, it's changed to 1 millisecond. Broadly speaking, a timer cannot span more than 24.8 days.

间隔值必须在1-2147483647的范围内(包含1和2147483647)。如果该值超出范围,则该值被当作1毫秒处理。一般来说,一个定时器不能超过24.8天。

Returns an opaque value that represents the timer.

返回一个代表该定时器的句柄值。

clearInterval(t)#

Stop a timer that was previously created with setInterval(). The callback will not execute.

停止一个之前通过setInterval()创建的定时器。回调不会再被执行。

The timer functions are global variables. See the timers section.

定制器函数是全局变量。见定时器章节。