javascript// console.log()用於標準輸入流的輸出
console.log('console');
// 輸入命令:node console.js
// 輸出結果:console
// 輸入命令:node console.js 1 > info.log
// 輸出結果:console信息輸入到info.log文件中
// 1表明重定向標準輸出流
// 輸入命令:node console.js 2 > error.log
// 2表明重定向標準錯誤輸出流
console.error('error');
// warn和error方法功能同樣
console.warn();
查看對象裏的內容並輸出:
javascript
var user = new Object();
user.name = 'mark';
user.getName = function() {
return this.name;
}
user.setName = function(name) {
this.name = name;
}
console.dir(user);
返回開始與結束之間的毫秒數:
javascript// console.time()標記開始時間
// console.timeEnd()標記結束時間
// 方法能夠傳入任何參數,但開始時間與結束時間參數必須一致
console.time('mark');
for (var i = 0; i < 100000; i++) {
console.log(i);
}
console.timeEnd('mark');
將當前位置的棧信息做爲標準錯誤信息進行輸出:
javascriptvar user = new Object();
user.name = 'mark';
user.getName = function() {
return this.name;
}
user.setName = function(name) {
this.name = name;
}
console.trace('trace');
對一個表達式結果進行評估,若是表達式爲false則輸出消息字符串並拋出AssertionError異常:
javascriptconsole.assert(1 == 1, 'assert');
顯示全部nodejs中的全局變量、函數、對象:
javascriptconsole.log(global);
輸出結果:
{ DTRACE_NET_SERVER_CONNECTION: [Function],
DTRACE_NET_STREAM_END: [Function],
DTRACE_NET_SOCKET_READ: [Function],
DTRACE_NET_SOCKET_WRITE: [Function],
DTRACE_HTTP_SERVER_REQUEST: [Function],
DTRACE_HTTP_SERVER_RESPONSE: [Function],
DTRACE_HTTP_CLIENT_REQUEST: [Function],
DTRACE_HTTP_CLIENT_RESPONSE: [Function],
global: [Circular],
process:
{ title: 'node',
version: 'v0.12.4',
moduleLoadList:
[ 'Binding contextify',
'Binding natives',
'NativeModule events',
'NativeModule util',
'NativeModule buffer',
'Binding buffer',
'Binding smalloc',
'NativeModule path',
'NativeModule module',
'NativeModule vm',
'NativeModule assert',
'NativeModule fs',
'Binding fs',
'Binding constants',
'NativeModule stream',
'NativeModule _stream_readable',
'NativeModule _stream_writable',
'NativeModule _stream_duplex',
'NativeModule _stream_transform',
'NativeModule _stream_passthrough',
'NativeModule smalloc',
'NativeModule console',
'Binding tty_wrap',
'NativeModule tty',
'NativeModule net',
'NativeModule timers',
'Binding timer_wrap',
'NativeModule _linklist',
'Binding cares_wrap',
'Binding uv',
'Binding pipe_wrap',
'Binding tcp_wrap',
'Binding stream_wrap',
'Binding signal_wrap' ],
versions:
{ http_parser: '2.3',
node: '0.12.4',
v8: '3.28.71.19',
uv: '1.5.0',
zlib: '1.2.8',
modules: '14',
openssl: '1.0.1m' },
arch: 'x64',
platform: 'darwin',
argv: [ 'node', '/Users/demacia/nodejs-mark/console.js' ],
execArgv: [],
env:
{ TMPDIR: '/var/folders/hz/gn_dy38x0g124hqsj9ymfxfh0000gn/T/',
XPC_FLAGS: '0x0',
Apple_PubSub_Socket_Render: '/private/tmp/com.apple.launchd.OMKUZQmKzc/Render',
TERM: 'xterm-256color',
SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.HU1zMSvblJ/Listeners',
TERM_PROGRAM: 'Apple_Terminal',
XPC_SERVICE_NAME: '0',
TERM_PROGRAM_VERSION: '343.7',
SHLVL: '2',
PWD: '/Users/demacia/nodejs-mark',
_: '/usr/local/bin/node',
TERM_SESSION_ID: 'FB18D939-2AFD-4756-B70E-9049EF73747C',
__CF_USER_TEXT_ENCODING: '0x1F5:0x19:0x34',
SHELL: '/bin/zsh',
HOME: '/Users/demacia',
LOGNAME: 'demacia',
USER: 'demacia',
PATH: '/Users/demacia/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin',
OLDPWD: '/Users/demacia',
ZSH: '/Users/demacia/.oh-my-zsh',
PAGER: 'less',
LESS: '-R',
LC_CTYPE: '',
LSCOLORS: 'Gxfxcxdxbxegedabagacad' },
pid: 6187,
features:
{ debug: false,
uv: true,
ipv6: true,
tls_npn: true,
tls_sni: true,
tls_ocsp: true,
tls: true },
_needImmediateCallback: false,
execPath: '/usr/local/bin/node',
debugPort: 5858,
_startProfilerIdleNotifier: [Function: _startProfilerIdleNotifier],
_stopProfilerIdleNotifier: [Function: _stopProfilerIdleNotifier],
_getActiveRequests: [Function: _getActiveRequests],
_getActiveHandles: [Function: _getActiveHandles],
reallyExit: [Function: reallyExit],
abort: [Function: abort],
chdir: [Function: chdir],
cwd: [Function: cwd],
umask: [Function: umask],
getuid: [Function: getuid],
setuid: [Function: setuid],
setgid: [Function: setgid],
getgid: [Function: getgid],
getgroups: [Function: getgroups],
setgroups: [Function: setgroups],
initgroups: [Function: initgroups],
_kill: [Function: _kill],
_debugProcess: [Function: _debugProcess],
_debugPause: [Function: _debugPause],
_debugEnd: [Function: _debugEnd],
hrtime: [Function: hrtime],
dlopen: [Function: dlopen],
uptime: [Function: uptime],
memoryUsage: [Function: memoryUsage],
binding: [Function: binding],
_linkedBinding: [Function: _linkedBinding],
_setupDomainUse: [Function: _setupDomainUse],
_events: { SIGWINCH: [Function] },
_rawDebug: [Function],
domain: null,
_maxListeners: undefined,
EventEmitter:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: false,
defaultMaxListeners: 10,
init: [Function],
listenerCount: [Function] },
_fatalException: [Function],
_exiting: false,
assert: [Function],
config: { target_defaults: [Object], variables: [Object] },
nextTick: [Function: nextTick],
_tickCallback: [Function: _tickCallback],
_tickDomainCallback: [Function: _tickDomainCallback],
stdout: [Getter],
stderr: [Getter],
stdin: [Getter],
openStdin: [Function],
exit: [Function],
kill: [Function],
addListener: [Function],
on: [Function],
removeListener: [Function],
mainModule:
{ id: '.',
exports: {},
parent: null,
filename: '/Users/demacia/nodejs-mark/console.js',
loaded: false,
children: [],
paths: [Object] } },
GLOBAL: [Circular],
root: [Circular],
Buffer:
{ [Function: Buffer]
poolSize: 8192,
isBuffer: [Function: isBuffer],
compare: [Function: compare],
isEncoding: [Function],
concat: [Function],
byteLength: [Function] },
setTimeout: [Function],
setInterval: [Function],
clearTimeout: [Function],
clearInterval: [Function],
setImmediate: [Function],
clearImmediate: [Function],
console: [Getter] }