class Point { constructor(x, y) { this.x = x; this.y = y; } }
console.log(new Point(10, 20));
Point { x: 10, y: 20 }
console.log(JSON.stringify(new Point(10, 20)))
{"x":10,"y":20}
const util = require(‘util‘); class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { const that = this; return JSON.stringify(that); } [util.inspect.custom](depth, options) { return this.toString() } }
const inspect = Symbol.for(‘nodejs.util.inspect.custom‘); class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { const that = this; return JSON.stringify(that); } [inspect]() { return this.toString() } }
前端技术之:如何在控制台将JS class实例输出为JSON格式
原文:https://www.cnblogs.com/popgis/p/11750179.html