nodejs>>util

/*util模块设计的主要目的是为了满足Node内部API的需求 。这个模块中的很多方法在你编写Node程序的时候都是很有帮助的。如果你觉得提供的这些方法满足不了你的需求,
    那么我们鼓励你编写自己的实用工具方法。我们 不希望util模块中添加任何对于Node的内部功能非必要的扩展*/


util.inherits(ctor, superCtor);
    //ctor继承来自superCtor的方法和属性
    eg:
    var util = require('util');
    function base(){
        this.base = '1991';
        this.name = 'base';
        this.sayHello = function(){
            console.log('Hello '+this.name);
        }
    }
    function sup(){
        this.name = 'sup';
    }
    base.prototype.showName = function(){
        console.log(this.name);
    }
    util.inherits(sup, base);
    var obj = new base();
    obj.showName();//base
    obj.sayHello();//Hello base
    console.log(obj);//{name:'base', base:'1991', sayHello:[function]}
    console.log();
    var obj = new sup();
    obj.showName();//sup
    //obj.sayHello();//这里会报错
    console.log(obj);//{name:'sup'}
    //如上面的例子util.inherits(sup, base);sup只继承base在原型prototype中定义的方法,而构造函数sayHello和base属性都没有被继承
    //同时在原型prototype中定义的属性不会被console.log做为对象的属性输出,如果去掉obj.sayHello();的注释,就会报错,因为sup就没有sayHello这个方法

 /*util.inspect(obj, showHidden, depth, colors);
    将obj对象转换为字符串,
    showHidden可选,是一个bool类型的参数,如果为true,则会输出更多的隐藏信息
    depth可选,为最大的地柜层数,如果对象有很多层的话,需要对输出信息的层数进行限制,默认为两层,如果要地柜到底,用null就可以了
    colors可选, 如果为true,输出格式就会以ANSI颜色编码,通常用于在终端显示更漂亮的效果
    这个方法不会简单的吧对象转换为字符串形式,即使对象定义了tostring方法也不会调用*/
    var util = require('util');
    function person(){
        this.name = 'yyf';
        this.totring = function(){
            return this.name;
        }
    }
    var obj = new person();

    console.log(util.inspect(obj));
    console.log(util.inspect(obj, true));
    输出
    { name: 'yyf', totring: [Function] }
    { name: 'yyf',
      totring:
       { [Function]
         [length]: 0,
         [name]: '',
         [arguments]: null,
         [caller]: null,
         [prototype]: { [constructor]: [Circular] } } }
    });

/*util.format(f,..);
   //根据第一个参数,返回一个格式化的字符串,类似printf
   f是一个字符串,包含零个或多个占位符,每个占位符占位符被替换为对应的被转换的值,支持的占位符有:
   %s - 字符串
   %d - 数字 (整型和浮点型)
   %j - JSON. 如果这个参数包含循环对象的引用,将会被替换成字符串 '[Circular]'
   %% - 单独一个百分号('%')。不会消耗一个参数*/
   var util = require('util');
   //如果占位符没有相对应的参数,占位符将不会被替换
   util.format('%s:%s', 'foo'); // 'foo:%s'
   //如果有多个参数占位符,额外的参数将会调用util.inspect()转换为字符串。这些字符串被连接在一起,并且以空格分隔
   util.format('%s:%s', 'foo', 'bar', 'baz'); // 'foo:bar baz'
   //如果第一个参数是一个非格式化字符串,那么util.format()将会把所有的参数转成字符串,以空格隔开,拼接在一块,并返回该字符串。util.inspect()会把每个参数都转成一个字符串。
   util.format(1, 2, 3); // '1 2 3'

util.log(string)
   在控制台进行输出,并带有时间戳。
   require('util').log('Timestamped message.');


util.isArray(object)
    如果给定的对象是数组类型,就返回true,否则返回false
    util.isArray([])
    // true
    util.isArray(new Array)
    // true
    util.isArray({})
    // false
    

util.isArray(object)
    如果给定的对象是数组类型,就返回true,否则返回false
    var util = require('util');
    console.log(util.isRegExp(/some regexp/));//true
    console.log(util.isRegExp(new RegExp('another regexp')));//true
    console.log(util.isRegExp({}));//false

util.isDate(object)
    如果给定的对象是Date类型,就返回true,否则返回false。
    var util = require('util');
    console.log(util.isDate(new Date()));//true
    console.log(util.isDate(Date()));//false 乜有关键词new返回一个字符串
    console.log(util.isDate({}));//false

util.isError(object)
    如果给定的对象是Error类型,就返回true,否则返回false。
    var util = require('util');
    console.log(util.isError(new Error()));//true
    console.log(util.isError(new TypeError()));//true
    console.log(util.isError({ name: 'Error', message: 'an error occurred' }));//false

util.debuglog(section)
    section {String} 被调试的程序节点部分
    返回值: {Function} 日志处理函数
    这个方法是在存在NODE_DEBUG环境变量的基础上,创建一个有条件写到stderr里的函数。
    如果“节点”的名字出现在这个环境变量里,那么就返回一个功能类似于console.error()的函数.如果不是,那么返回一个空函数.
    var bar = 123; debuglog('hello from foo [%d]', bar); ```
    如果这个程序以NODE_DEBUG=foo 的环境运行,那么它将会输出:
    FOO 3245: hello from foo [123]
    3245是进程的ID, 如果程序不以刚才那样设置的环境变量运行,那么将不会输出任何东西。
    多个NODE_DEBUG环境变量,你可以用逗号进行分割。例如,NODE_DEBUG= fs, net, tls。

    自定义 util.inspect 颜色
    util.inspect彩色输出(如果启用的话) ,可以通过util.inspect.styles 和 util.inspect.colors 来全局定义。
    util.inspect.styles是通过util.inspect.colors分配给每个风格颜色的一个映射。 高亮风格和它们的默认值:
    number (黄色) boolean (黄色) string (绿色) date (洋红色) regexp (红色) null (粗体) undefined (灰色) 
    special - 在这个时候的唯一方法 (青绿色) * name (无风格)
    预定义的颜色代码: white, grey, black, blue, cyan, green, magenta, red 和 yellow。 还有 bold, italic, underline 和 inverse 代码。

    自定义对象的inspect()方法
    对象可以定义自己的 inspect(depth)方法;当使用util.inspect()检查该对象的时候,将会执行对象自定义的检查方法。
    util.inspect(obj);
    // "{nate}"
    您也可以返回完全不同的另一个对象,而且返回的字符串将被根据返回的对象格式化。它和JSON.stringify()工作原理类似:
    util.inspect(obj);
    // "{ bar: 'baz' }"

    util.debug(string)
    已过时: 请使用 console.error() 代替
    console.error的已过时的前身

    util.error([...])
    已过时: 请使用 console.error() 代替
    console.error的已过时的前身

    util.puts([...])
    已过时: 请使用 console.log() 代替
    console.log的已过时的前身

    util.print([...])
    已过时: 请使用 console.log() 代替
    console.log的已过时的前身

    util.pump(readableStream, writableStream, [callback])
    已过时: 请使用readableStream.pipe(writableStream)代替
    stream.pipe()的已过时的前身







asd





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值