将Javascript对象(包括函数)转换为String [英] Convert Javascript Object (incl. functions) to String
问题描述
嘿,我试图将特定的javascript对象转换为String。到目前为止,我正在使用json2.js。只有我的对象包含函数,这些函数才会被剥离。我需要一种转换函数的方法,任何想法?
Hey, Im trying to convert specific javascript objects to a String. So far I'm working with json2.js. As soon as my Object contain functions, those functions are stripped. I need a way to convert functions too, any ideas?
firefox中的函数有一个toString()方法,但是如何使用json2.js?
There is a toString() method for functions in firefox, but how to make that work with json2.js?
推荐答案
实际上,我认为这是可能的,也很容易。至少在使用nodeJS做jsonP时,它对我很有用,并且可以通过以下小提琴来证明。
我只是通过向函数添加字符串来实现:
Actually, I think it is possible and easy. At least when doing jsonP with nodeJS it works for me just fine, and it's demonstratable by the following fiddle. I did it by simply adding strings to a function:
var anyString = '';
var aFunction = function() { return true; };
var functionToText = anyString + aFunction;
console.log(functionToText);
这里是小提琴: http://jsfiddle.net/itsatony/VUZck/
这篇关于将Javascript对象(包括函数)转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!