此篇文章瀏覽量:
701
util 模組中的 inspect() 方法,可以將任何物件,轉換成字串,以便進行操作。
將物件轉換成字串
var util = require('util'); // 引入常用工具(util)模組
// 定義原型類別 Person
function Person() {
this.name = 'person';
this.toString = function() {
return this.name;
};
}
// 定義 Person 物件
var obj = new Person();
console.log(util.inspect(obj));
console.log(util.inspect(obj, true));
輸出結果如下圖: