I know it is possible to convert a JavaScript data object to JSON using JSON.stringify. e.g:
var cat = {
soundString: 'Meow!'
};
Then call JSON.stringify(cat) to get "{"soundString":"Meow!"}"
I am interested to know if it is possible to have a parallel to this, except instead of turning the JavaScript object into JSON, turn it into a JavaScript string that can be evaluated back to the object.
var cat = {
meow: function() {
console.log('Meow!');
}
};
I want something that would take the cat object and produce a string "{meow: function(){console.log('Meow!');}}", which can be parsed back using eval.
Can this be accomplished?
toString()method. You would either have to invent your own serialization format or use actual Javascript that the other end would runeval()on (with the inherent risks of doing that).