您的位置:首页 > Web前端 > Node.js

nodejs 常用工具 util

2016-06-21 15:06 495 查看
util 是一个 nodejs 核心模块,提供常用函数的集合

1.util.inherits
util.inherits 是一个实现对象间原型继承的函数

var util = require("util");
function Base(){
        this.name = 'base';
        this.base = 1991;
        this.sayHello = function(){
                console.log("Hello " + this.name);
        };
}

Base.prototype.showName = function(){
        console.log(this.name);
};

function Sub(){
        this.name = 'sub';
}

util.inherits(Sub, Base);
var objBase = new Base();

objBase.showName();
objBase.sayHello();
console.log(objBase);

var objSub = new Sub();

objSub.showName();
console.log(objSub);

输出结果:

base
Hello base
Base { name: 'base', base: 1991, sayHello: [Function] }
sub
Sub { name: 'sub' }

继承只能继承共有的函数跟变量。

2.util.inspect

util.inspect(object,[showHidden],[depth],[colors])是一个将任意对象转换 为字符串的方法,通常用于调试和错误输出。它至少接受一个参数 object,即要转换的对象。

showHidden 是一个可选参数,如果值为 true,将会输出更多隐藏信息。

depth 表示最大递归的层数,如果对象很复杂,你可以指定层数以控制输出信息的多 少。如果不指定depth,默认会递归2层,指定为 null 表示将不限递归层数完整遍历对象。 如果color 值为 true,输出格式将会以ANSI 颜色编码,通常用于在终端显示更漂亮 的效果。

特别要指出的是,util.inspect 并不会简单地直接把对象转换为字符串,即使该对 象定义了toString 方法也不会调用。

var util = require("util");
function Person(){
        this.name = 'byvoid';
        this.toString = function(){
                return this.name;
        };
}
var obj = new Person();
console.log(util.inspect(obj));
console.log(util.inspect(obj, true));
console.log(util.inspect(obj, true, 1, true));

3.util.isArray(object)
如果给定的参数 "object" 是一个数组返回true,否则返回false

util.isArray([])//true
util.isArray(new Array)//true
util.isArray({})//false

4.util.isRegExp(object)
如果给定的参数 "object" 是一个正则表达式返回true,否则返回false

var util = require("util");
util.isRegExp(/some regexp/) //true
util.isRegExp(new RegExp('another regexp')) //true
util.isRegExp({})//false

5.util.isDate(object)
如果给定的参数 "object" 是一个日期返回true,否则返回false

util.isDate(new Date())// true
util.isDate(Date())//false without 'new' returns a string
util.isDate({})//false

6.util.isError(object)
如果给定的参数 "object" 是一个错误对象返回true,否则返回false
util.isError(new Error())//true
util.isError(new TypeError())// true
util.isError({name:"Error", message:"an error occurred"})//false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: