首页 > Web开发 > 详细

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

时间:2021-08-24 16:59:46      阅读:7      评论:0      收藏:0      [点我收藏+]
  • 在对象中重写 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));

  • 结果

技术分享图片

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

原文:https://www.cnblogs.com/chilli-with-fish/p/15180716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!