JS | TS 自定义 object 的 json 序列化

  • 在对象中重写 toJSON 实现 自定义stringify
class Point{
  x = 0;
  y = 0;
  z = 0;

  toJSON(){
    return [this.x,this.y];
  }
}

let point = new Point();
console.log(JSON.stringify(point));

  • 结果

posted @ 2021-08-24 15:54  辣椒鱼儿酱  阅读(1008)  评论(0)    收藏  举报