суббота, 7 ноября 2015 г.

ES6, how to avoid stack overflow in toJSON method implementation

class C {
   construcror() {
      this.id = 0;
  }
  toJSON() {
    // this causes stack overflow
    return JSON.stringify(this);
  }
}

Object.assign helps you

// this is right :)

toJSON() {
  return JSON.stringify(Object.assign({}, this));
}

Комментариев нет:

Отправить комментарий