zoukankan      html  css  js  c++  java
  • node.js基础知识

    1.node.js中的console对象

    1.1console.log():用于输出信息

    console.log('This is a test string')//控制台输出: This si a text string

    将代码保存在一个命名为app.js的脚本文件中,将输出的字符串保存到 info.log中,可进行如下操作:

    node app.js 1>indo.log//1代表重定向标准输出流

    console.log()方法中通过参数输出字符串格式,具体如下代码所示:

    console.log("%s","hoge","foo")//依次输出所有字符串
    console.log("%d"10,10.3)//将数值转换为字符串
    console.log("%d","string")//将字符串作为字符串输出
    console.log("%%","hoge")//输出%hoge

    console.log()中可以使用运算符进行结果计算: console.log("2+2")//4 node.js中可以使用console.info方法代替console.log方法

    1.2 console.error():用于错误信息的输出

    可以将错误信息输出到指定的文件中:

    node app.js 2>error.log//2代表重定向标准错误输出流

    1.3console.dir():查看对像内容并将其输出到控制台

     1 var user = new Object()
     2 user.name = 'yuanshen'
     3 user.getName = function(){
     4                 return this.name
     5         }
     6 user.setName = function(name){
     7                 this.name = name
     8         }
     9 console.dir(user);

    输出结果:

    { name: 'yuanshen', getName: [Function], setName: [Function] }

    1.4 console.time方法与console.timeEnd方法

    console.time(label):用于标记开始时间
    console.timeEnd(label):用于标记结束时间
    注意:参数必须保持一致,所计时单位为ms
    eg:

      1 function texttime(){
      2 for(var i=0;i<100000;i--){
      3 }
      4 }   
      5 console.time('texttime')
      6 console.timeEnd('texttime')

    输出结果:

    texttime: 0.235ms
    

    1.5 console.trace方法:用于将当前栈信息作为标准错误信息输出

    使用方法:console.trace(label)
    eg:

      1 var user = new Object()
      2 user.name = 'yuanshen'
      3 user.getName = function(){ 
      4  return this.name
      5   }
      6 user.setName = function(name){
      7   this.name = name
      8   }
      9 console.trace(trace)

    输出结果:

       /root/webproject/text1.js:9
    console.trace(trace)
      ^
    
    ReferenceError: trace is not defined
    at Object.<anonymous> (/root/webproject/text1.js:9:15)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

    1.6 console.assert方法用于对表达式结果进行屏评估,如果返回false,则输出一个消息字符串并抛出AssertionError异常

    eg:

    > console.assert(1==2,'等式不成立')
    AssertionError: 等式不成立
    at Console.assert (console.js:95:23)
    at repl:1:9
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:538:10)
    at emitOne (events.js:101:20)
  • 相关阅读:
    关于数据库的索引知识
    RESTful API设计相关
    Coroutine(协程)模式与线程
    Python网络编程中的服务器架构(负载均衡、单线程、多线程和同步、异步等)
    读懂diff
    Linux学习笔记——如何使用echo指令向文件写入内容
    ubuntu中执行定时任务crontab
    网络编程之异步IO,rabbitMQ笔记
    走进docker的世界之入门篇
    xml基础
  • 原文地址:https://www.cnblogs.com/yuanchenghao/p/7403007.html
Copyright © 2011-2022 走看看