gpt4 book ai didi

javascript - JSON.stringify如何自动将矩对象转换为iso字符串?

转载 作者:行者123 更新时间:2023-12-02 07:17:13 25 4
gpt4 key购买 nike

我很好奇,像moment这样的库如何在对象上调用JSON.stringify时自动从对象转换为字符串。

此刻的示例测试:https://github.com/moment/moment/blob/3147fbc486209f0b479dc0b29672d4c2ef39cf43/src/test/moment/format.js#L144-L146

这是一些我很好奇它如何工作的示例代码

const moment = require('moment');
const duration = moment.duration(1374);
console.log('duration = ', duration); // Prints a duration object
console.log('JSON.stringify(duration) = ', JSON.stringify(duration)); // Prints a string such as `P0D1T0H3` <-- Note: Not exact value, just similar format

最佳答案

MDN:

If the value has a toJSON() method, it's responsible to define what data will be serialized.



moment:

proto.toJSON         = toISOString;

关于javascript - JSON.stringify如何自动将矩对象转换为iso字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com