[NODE之8]Util模块

来源:互联网 发布:mac迅雷登陆环境异常 编辑:程序博客网 时间:2024/05/22 10:49
/** * Created by liyanq on 17/3/22. */const util = require("util");const EventEmitter = require('events');/**util.deprecate(function, string) * util.deprecate() 方法会包装给定的 function 或类,并标记为废弃的 * 开来这个很方便用到exports上面,通过控制台告诉别人,这个方法不要了。*/var funDeprecate = function () {    for (var i = 0, len = arguments.length; i < len; ++i) {        process.stdout.write(arguments[i] + '\n');    }};var afterFunDeprecate = util.deprecate(funDeprecate, "我是funDeprecate被废弃的理由~");// funDeprecate("我是直接调用的参数");if (typeof afterFunDeprecate === "function") {    afterFunDeprecate("我是被废弃后的参数");}/*----------------------------------------------------------------------------------*//**util.format(format[, ...args]) * 如果传入 util.format() 方法的参数比占位符的数量多,则多出的参数会被强制转换为字符串( * 对于对象和符号,使用 util.inspect()),然后拼接到返回的字符串,参数之间用一个空格分隔。 * util.inspect()很犀利,等下看看~~ */util.format('%s:%s', 'foo', 'bar', 'baz'); //foo:bar baz 第三个被强制转换了/*----------------------------------------------------------------------------------*//**util.inherits(constructor, superConstructor) * 注意,不建议使用 util.inherits()。请使用 ES6 的 class 和 extends 关键词获得语言层面的继承支持。 * 注意,这两种方式是语义上不兼容的。 * constructor <Function> * superConstructor <Function>从一个构造函数中继承原型方法到另一个。 * constructor 的原型会被设置到一个从 superConstructor 创建的新对象上。 * 看来,ES6还是很有必要学的~~ * */function MyStream() {    EventEmitter.call(this);//构造方法的调用;不写也可以~}util.inherits(MyStream, EventEmitter);//prototype的复制// MyStream.prototype = EventEmitter.prototype;//如果这样写,MyStream.super_ === EventEmitter为false;MyStream.prototype.write = function (data) {//有则覆盖,无则新建~    this.emit("data", data);//这里有警告};var stream = new MyStream();stream.on('data', function (data) {    console.log("接收的数据:" + data);});stream.write('运作良好!');/*----------------------------------------------------------------------------------*/class MyStream2 extends EventEmitter {    constructor() {        super();    }    write(data) {        this.emit('data', data);    }}const stream2 = new MyStream2();stream2.on('data', (data)=> {    console.log(`接收的数据:"${data}"`);});stream2.write('使用 ES6');/**语法:util.inspect(object[, options]);是一个将任意对象转换 为字符串的方法 * 这个厉害了,应该util模块就指望着这个呢~~~ * object <any> 任何 JavaScript 原始值或对象。 * options <Object> *     showHidden <boolean> 如果为 true,则 object 的不可枚举的符号与属性也会被包括在格式化后的结果中。 默认为 false。 *     depth <number> 指定格式化 object 时递归的次数。 这对查看大型复杂对象很有用。 默认为 2。 若要无限地递归则传入 null。 *     colors <boolean> 如果为 true,则输出样式使用 ANSI 颜色代码。 默认为 false。 颜色可自定义,详见自定义 util.inspect 颜色。 *     customInspect <boolean> 如果为 false,则 object 上自定义的 inspect(depth, opts) 函数不会被调用。 默认为 true。 *     showProxy <boolean> 如果为 true,则 Proxy 对象的对象和函数会展示它们的 target 和 handler 对象。 默认为 false。 *     maxArrayLength <number> 指定格式化时数组和 TypedArray 元素能包含的最大数量。 默认为 100。 设为 null 则显式全部数组元素。 设为 0 或负数则不显式数组元素。 *     breakLength <number> 一个对象的键被拆分成多行的长度。 设为 Infinity 则格式化一个对象为单行。 默认为 60。*///查看 util 对象的所有属性:util.inspect(util, {showHidden: true, depth: null});const objInspect = {    name: "hello",    age: 30,    info: {        name: "world",        age: 34    }};const objInspect2 = new Object("Hello World");objInspect2.info = objInspect;objInspect.address = "北京";util.inspect(objInspect2, {showHidden: true});//能显示[length]: 11,/*{ [String: 'Hello World'] info: { name: 'hello', age: 30, info: [Object], address: '北京' } } */util.inspect(objInspect2, {showHidden: false, depth: 1, colors: true});//只显示一层,depth默认是2/*{ [String: 'Hello World'] info: { name: 'hello', age: 30, info: { name: 'world', age: 34 }, address: '北京' } } */util.inspect(objInspect2, {showHidden: false, depth: 2, colors: true});/*customInspect这个默认为true,意思是能够自定义自己的函数inspect,类似oc里面的description * options.stylize这个是什么????*/class Box {    constructor(value) {        this.value = value;    }    inspect(depth, options) {//这个可以在prototype中覆盖~        const inner = util.inspect(this.value, options);        return 'Box' + '<' + inner + '>';    }}const box = new Box("hello");util.inspect(box, {colors: true, showHidden: true});//Box< 'hello' >/*util.inspect.custom这个属性是个函数*/box[util.inspect.custom] = function(depth,options) {//只是对象级别的    return { bar: 'baz' };};util.inspect(box, {colors: true, showHidden: true});//{ bar: 'baz' }const box1 = new Box("world");util.inspect(box1, {colors: true, showHidden: true});//Box< 'world' >Box.prototype[util.inspect["custom"]]= function (depth,options) {//类级别的,影响以后的所有对象~    var s = this.inspect(depth,options);//对象的inspect方法    return { bar: 'baz1',s:s };};const box2 = new Box("world");util.inspect(box2, {colors: true, showHidden: true});//{ bar: 'baz1', s: '\u001b[32m\'world\'\u001b[39m' }

最后个结果怎么出来的这个?不清楚原因

参考:http://nodejs.cn/api/util.html#util_util_isstring_object

0 0
原创粉丝点击