Peter David Carter-Poulsen Peter David Carter-Poulsen - 6 months ago 12
JSON Question

Using toJSON() in Backbone.js

I am trying to find out how to properly use the

toJSON()
function in Backbone.js for an online tutorial. The tutorial states:


If you don't remember how toJSON works, consult the Backbone Model docs.


However, when I follow the link and click on toJSON in the left sidebar, it just talks about
JSON.stringify
instead of
toJSON()
. Can someone tell me the correct usage for the
toJSON()
function?

Answer

toJSON() is basic javascript.

Using toJSON()

var jsonDate = (new Date()).toJSON();
var backToDate = new Date(jsonDate);

console.log(jsonDate); //2015-10-26T07:46:36.611Z

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON