Implementing Mozilla's toSource() method in Internet Explorer

后端 未结 8 844
轮回少年
轮回少年 2020-11-27 07:41

Has anyone implemented Mozilla\'s Object.toSource() method for Internet Explorer and other non-Gecko browsers? I\'m looking for a lightweight way to serialize simple object

相关标签:
8条回答
  • 2020-11-27 07:59

    You don't have to use toSource(); wrap the code to be serialized in a function that returns the JSON struct, and use function#toString() instead:

    var serialized = function () {
        return {
            n: 8,
            o: null,
            b: true,
            s: 'text',
            a: ['a', 'b', 'c'],
            f: function () {
                alert('!')
            }
        }
    };
    serialized.toString();
    

    See a live demo on jsFiddle.

    0 讨论(0)
  • 2020-11-27 08:03

    If matching the exact serialization format of Firefox is not your aim, you could use one of the JavaScript JSON serialization/deserialization libraries listed at http://json.org. Using a standard scheme like JSON may be better than mimicking the proprietary Gecko format.

    0 讨论(0)
提交回复
热议问题