If you clone a moment using other libraries such as underscorejs' clone method, or knockoutjs' toJS method and then try to convert this back to a moment you get an infinite recursion.
Steps to reproduce
moment(_.clone(moment()));
Example
A more realistic case where this happens is if you use something like knockout.postbox which internally calls JSON.stringify(ko.toJS(value)).
- ko.toJS clones the moment to an object but retains the toJSON method.
- JSON.stringify then tries to call the toJSON method.
- Moment's toJSON method then tries to clone this before converting to utc. Unfortunately this is no longer a moment.
The code for this would look something like this.
var m = new moment();
var data = {when: m};
ko.postbox.publish('something.happened', data);
This makes it far less obvious that the moment has been cloned to a plain object.
Expected behaviour
Either moment should support moments that have been cloned incorrectly by other libraries, or a more meaningful exception should be thrown rather than causing a stack overflow.
If you clone a moment using other libraries such as underscorejs' clone method, or knockoutjs' toJS method and then try to convert this back to a moment you get an infinite recursion.
Steps to reproduce
Example
A more realistic case where this happens is if you use something like knockout.postbox which internally calls JSON.stringify(ko.toJS(value)).
The code for this would look something like this.
This makes it far less obvious that the moment has been cloned to a plain object.
Expected behaviour
Either moment should support moments that have been cloned incorrectly by other libraries, or a more meaningful exception should be thrown rather than causing a stack overflow.