Created
September 19, 2012 13:45
-
-
Save cowboy/3749767 to your computer and use it in GitHub Desktop.
Revisions
-
cowboy created this gist
Sep 19, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ var stringify = function(obj, prop) { var placeholder = '____PLACEHOLDER____'; var fns = []; var json = JSON.stringify(obj, function(key, value) { if (typeof value === 'function') { fns.push(value); return placeholder; } return value; }, 2); json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function(_) { return fns.shift(); }); return 'this["' + prop + '"] = ' + json + ';'; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ var foo = { a: function() { return 'a'; }, b: function() { return 'b'; }, 'bar.baz': { 'omg ponies!!': function() { return 'c'; } } }; console.log(stringify(foo, 'foo')); // this["foo"] = { // "a": function () { return 'a'; }, // "b": function () { return 'b'; }, // "bar.baz": { // "omg ponies!!": function () { return 'c'; } // } // };