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
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
相关文章推荐
- Node.js 全局对象
- nodejs 路由
- nodejs 函数
- nodejs 事件
- nodejs 模块系统
- 安装 node
- hibernate 中,出现了错误 "node to traverse cannot be null!" 如何改正
- (转)nodejs中npm常用命令
- LeetCode Reverse Nodes in k-Group(单链表连续分段反转)
- ROS学习--人脸识别NODE
- JetBrains使用技能分享征集大赛优秀作品——用webstorm开发NodeJS项目
- ROS学习--语义理解NODE(NLU,NLP)
- systemtap [主设备号,次设备好,inode]监控文件
- LeetCode OJ | #25 Reverse Nodes in k-Group
- nodejs的require模块及路径
- Nodejs的测试和测试驱动开发
- Node.js模块定义总结
- node环境
- node应用线上部署时锁定包的依赖版本
- Nodejs与Python脚本语言混编