const util = require('util'); console.log(util.format('您输入了%d个参数,参数值分别为%s,%s,%s', 3, 'nice', 'excelent', 'holy')); //您输入了3个参数,参数值分别为nice,excelent,holy console.log(util.format('一个JSON对象%j', {'name': 'jack', 'age': 25})); // 一个JSON对象{"name":"jack","age":25} console.log(util.format('一个百分号%'));// 一个百分号% console.log(util.format('%s:%s', 'one'));// one:%s console.log(util.format('%s', 'one', 'two', 'three', {'name': 'jack'})); function test(one, two) { return one + two; } let parent = new Object(); parent.name = 'parent'; parent.func = test; let child1 = new Object(); child1.name = 'child1'; parent.child1 = child1; let child2 = new Object(); child2.name = 'child2'; child1.child = child2; let child3 = new Object(); child3.name = 'child3'; child2.child = child3; child2.inspect = function (depth) { return util.inspect(this, {depth: depth - 2, customInspect: false}) }; console.log(util.inspect(parent, {customInspect: true, depth: 4})); /** * { name: 'parent', * func: [Function: test], * child1: * { name: 'child1', * child: { name: 'child2', child: [Object], inspect: [Function] } } } * **/
Node.js readline模块与util模块的使用(2)
内容版权声明:除非注明,否则皆为本站原创文章。