当前位置:天才代写 > tutorial > 其他教程 > Node.js 常用东西

Node.js 常用东西

2017-11-02 08:00 星期四 所属: 其他教程 浏览:702

Node.js 常用东西

util 是一个Node.js 焦点模块,提供常用函数的荟萃,用于补充焦点JavaScript 的成果 过于精简的不敷。


util.inherits

util.inherits(constructor, superConstructor)是一个实现工具间原型担任 的函数。

JavaScript 的面向工具特性是基于原型的,与常见的基于类的差异。JavaScript 没有 提供工具担任的语言级别特性,而是通过原型复制来实现的。

在这里我们只先容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(); 
//objSub.sayHello(); 
console.log(objSub); 

我们界说了一个基本工具Base 和一个担任自Base 的Sub,Base 有三个在结构函数 内界说的属性和一个原型中界说的函数,通过util.inherits 实现担任。运行功效如下:

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

留意:Sub 仅仅担任了Base 在原型中界说的函数,而结构函数内部缔造的 base 属 性和 sayHello 函数都没有被 Sub 担任。

同时,在原型中界说的属性不会被console.log 作 为工具的属性输出。假如我们去掉 objSub.sayHello(); 这行的注释,将会看到:

node.js:201 
throw e; // process.nextTick error, or 'error' event on first tick 
^ 
TypeError: Object #<Sub> has no method 'sayHello' 
at Object.<anonymous> (/home/byvoid/utilinherits.js:29:8) 
at Module._compile (module.js:441:26) 
at Object..js (module.js:459:10) 
at Module.load (module.js:348:31) 
at Function._load (module.js:308:12) 
at Array.0 (module.js:479:10) 
at EventEmitter._tickCallback (node.js:192:40) 

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)); 

运行功效是:

Person { name: 'byvoid', toString: [Function] }
Person {
  name: 'byvoid',
  toString: 
   { [Function]
     [length]: 0,
     [name]: '',
     [arguments]: null,
     [caller]: null,
     [prototype]: { [constructor]: [Circular] } } }

util.isArray(object)

假如给定的参数 “object” 是一个数组返回true,不然返回false。

var util = require('util');

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

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

util.isDate(object)

假如给定的参数 “object” 是一个日期返回true,不然返回false。

var util = require('util');

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

util.isError(object)

假如给定的参数 “object” 是一个错误工具返回true,不然返回false。

var util = require('util');

util.isError(new Error())
  // true
util.isError(new TypeError())
  // true
util.isError({ name: 'Error', message: 'an error occurred' })
  // false

更多详情可以会见 http://nodejs.org/api/util.html 相识具体内容。

 

    关键字:

天才代写-代写联系方式