web杂记(7)-js(3)

发布时间 2023-08-23 11:13:33作者: 水宝石

目录

toJSON

b = {
  x: 11,
  y: 12,

  valueOf: function() {
    return Math.sqrt(this.x ** 2 + this.y ** 2);
  },
  toJSON: function() {
    return `{x:${this.x},y:${this.y},z:${Math.sqrt(this.x ** 2 + this.y ** 2)}}`;
  }
};
console.log(b.toJSON());
console.log(JSON.stringify(b));

b = {
  x: 11,
  y: 12,

  valueOf: function() {…
{x:11,y:12,z:16.278820596099706} debugger eval code:12:9
"{x:11,y:12,z:16.278820596099706}"