nodejs学习笔记-console基本方法的使用

989 查看

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] }