Skip to content

Instantly share code, notes, and snippets.

@cloudchen
Created April 10, 2013 07:59
Show Gist options
  • Save cloudchen/5352689 to your computer and use it in GitHub Desktop.
Save cloudchen/5352689 to your computer and use it in GitHub Desktop.
stringify javascript variable >> It's useful when you want write javascript variable into html template >> It's much better than JSON.stringify() since that doesn't support stringify function data type
function stringify(obj, variable) {
var objLiteral = '';
if (typeof obj == 'object') {
objLiteral += variable + "=";
if (obj.constructor == Array) {
objLiteral += "[];\n";
} else {
objLiteral += "{};\n";
}
for (var item in obj) {
if (typeof obj[item] == 'object') {
objLiteral += stringify(obj[item], variable + "['" + item + "']");
} else if (typeof obj[item] == 'function') {
objLiteral += variable + "['" + item + "'] = " + obj[item].toString() + ";\n"
} else if (typeof obj[item] == 'string') {
objLiteral += variable + "['" + item + "'] = '" + obj[item].toString() + "';\n"
} else {
objLiteral += variable + "['" + item + "'] = " + obj[item].toString() + ";\n";
}
}
} else if(typeof obj == "string") {
objLiteral += variable + "='" + obj.toString() + "';\n";
} else {
objLiteral += variable + "=" + obj.toString() + ";\n";
}
return objLiteral;
}
@cloudchen
Copy link
Author

transfer complicated variable into its literal type

var obj = {
    num: 1,
    str: "cloud",
    bool: true,
    arr: [1,"cloud",false, function(arg,arg2){alert(arguments)}, {name:"cloud"}],
    nested_arr: [1,"cloud",false, function(arg,arg2){alert(arguments)}, {name:"cloud"}, [2,"chen",true, function(arg3,arg4){alert(arguments.length)}, {name:"chen"}]],
    fun: function(a, b) {
        console.log(a, b);
    },
    obj: {
        num: 1,
        nested: {
            aa: 2,
            bb: function() {
                alert(arguments);
            },
            cc: [
                'a', 'r', 'r', 'a', 'y'
            ]
        }
    }
}

var result = stringify(obj, 'config');
console.log(result);
// or
eval(result)

This is how the result look like

config={};
config['num'] = 1;
config['str'] = 'cloud';
config['bool'] = true;
config['arr']=[];
config['arr']['0'] = 1;
config['arr']['1'] = 'cloud';
config['arr']['2'] = false;
config['arr']['3'] = function (arg,arg2){alert(arguments)};
config['arr']['4']={};
config['arr']['4']['name'] = 'cloud';
config['nested_arr']=[];
config['nested_arr']['0'] = 1;
config['nested_arr']['1'] = 'cloud';
config['nested_arr']['2'] = false;
config['nested_arr']['3'] = function (arg,arg2){alert(arguments)};
config['nested_arr']['4']={};
config['nested_arr']['4']['name'] = 'cloud';
config['nested_arr']['5']=[];
config['nested_arr']['5']['0'] = 2;
config['nested_arr']['5']['1'] = 'chen';
config['nested_arr']['5']['2'] = true;
config['nested_arr']['5']['3'] = function (arg3,arg4){alert(arguments.length)};
config['nested_arr']['5']['4']={};
config['nested_arr']['5']['4']['name'] = 'chen';
config['fun'] = function (a, b) {
            console.log(a, b);
        };
config['obj']={};
config['obj']['num'] = 1;
config['obj']['nested']={};
config['obj']['nested']['aa'] = 2;
config['obj']['nested']['bb'] = function () {
                    alert(arguments);
                };
config['obj']['nested']['cc']=[];
config['obj']['nested']['cc']['0'] = 'a';
config['obj']['nested']['cc']['1'] = 'r';
config['obj']['nested']['cc']['2'] = 'r';
config['obj']['nested']['cc']['3'] = 'a';
config['obj']['nested']['cc']['4'] = 'y';

@cloudchen
Copy link
Author

It supports primitive data type

var a = stringify(function(asd,bb){asd}, 'config');
console.log(a);

var a = stringify("asdf", 'config');
console.log(a);

var a = stringify(12, 'config');
console.log(a);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment