[removed] stringify object (including members of type function)

后端 未结 5 999
温柔的废话
温柔的废话 2020-12-01 10:44

I\'m looking for a solution to serialize (and unserialize) Javascript objects to a string across browsers, including members of the object that happen to be functions. A typ

5条回答
  •  离开以前
    2020-12-01 11:31

    You can use JSON.stringify with a replacer like:

    JSON.stringify({
       color: 'red',
       doSomething: function (arg) {
            alert('Do someting called with ' + arg);
       }
    }, function(key, val) {
            return (typeof val === 'function') ? '' + val : val;
    });
    

提交回复
热议问题