// First, create your object to be stringified
var j = {
dat: [123, 456, 789],
cb: function(){
alert("test");
}
};
// Then stringify it, with a slight modification
var s = JSON.stringify(j, function(key, val){
// when we find a function, we need to make it a string
if(typeof(val) == "function"){
return "" + val;
}
return val;
});
//console.log(s);
// To get the function back, parse the JSON with a slight modification
var loaded = JSON.parse(s, function(key, val){
// If we can match what a general function looks like
if(("" + val).match(/^function[ ]?\(\){([^\0]*)?}$/)){
// return the evaluated form of the function
return eval("(" + val + ")");
}
return val;
});
// And now you can call it.
//console.log(loaded.cb);
loaded.cb();
So, what gems do you have in relation to Javascript?

New Topic/Question
Reply



MultiQuote




|