Saturday, 15 February 2014

javascript - How does moment.js know when it's object is being serialised? -



javascript - How does moment.js know when it's object is being serialised? -

from moment.js docs

moment().tojson(); when serializing object json, if there moment object, represented iso8601 string.

json.stringify({ postdate : moment() }); // {"postdate":"2013-02-04t22:44:30.652z"}

i don't understand how moment object can observe function operating on it. how able homecoming different value when serialised, , when stored in object, or returned string?

when using stringify, object may define how gets represented, shown in documentation:

from https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/json/stringify

tojson behavior

if object beingness stringified has property named tojson value function, tojson method customizes json stringification behavior: instead of object beingness serialized, value returned tojson method when called serialized. example: var x = { foo: 'foo', tojson: function () { homecoming 'bar'; } }; var json = json.stringify({x: x}); //json string '{"x":"bar"}'.

moment.js's documentation (seen here: https://raw.github.com/timrwood/moment/2.0.0/moment.js ) shows indeed supported, here exact code

tojson : function () { homecoming moment.utc(this).format('yyyy-mm-dd[t]hh:mm:ss.sss[z]'); }

so, how aware of how represent when beingness stringified.

javascript date datetime momentjs

No comments:

Post a Comment