node.js中的util工具主要作用是满足平台内部的API需求 。下面介绍该模块中的主要方法:
调用方法:var outil=require('util');
util.inherits(constructor,superConstructor)
继承原型对象上的方法:
var outil=require('util');
function Cat()
{
this.name='Tom';
this.age=14;
this.food='鱼';
this.interest=function(){
console.log(this.name+"爱吃"+this.food);
}
}
Cat.prototype.showName=function(){
console.log(this.name);
}
function Mouse()
{
this.name='Jerry';
}
outil.inherits(Mouse,Cat);
var ocat=new Cat();
ocat.interest();
ocat.showName();
console.log(ocat);
var omouse=new Mouse();
omouse.showName();
console.log(omouse);
执行结果:
上例说明Mouse只是继承了Cat在原型上定义的函数,而Cat构造函数内部的属性和函数都没有被继承。