我想转储这个数组到字符串,并在评估它后得到相同的对象。如何转储JavaScript数据与函数评估字符串?
arr = [5, 86, function() { console.log(arguments); }, [console.log, 357]];
虽然函数可能有额外的属性,但只有代码是必要的重现。
我不能找到一种方法,在节点做到这一点:
> console.log(arr)
[5, 86, [Function] [ [Function], 357] ]
> util.format('%j', arr)
'[5,86,null,[null,357]]'
> arr.toString()
'5,86,function() { console.log(arguments); },function() {\n process.stdout.write(util.format.apply(this, arguments) + \'\\n\');\n},357'
后者已flatterned数组。
有没有更好的方法?
更新:找到了满意的解决方案,看到了自己的答案。
我已经改变了问题的标题。 :) – 2012-02-17 18:29:18
我认为它可以代表功能。或者它可以吗? 因为我以前存储在MongoDB中 – 2012-02-17 18:38:41
也许BSON能的功能,但是JSON不能:它适合在一个屏幕上:http://json.org – 2012-02-17 18:46:22