Last Updated: February 25, 2016
·
375
· littleche90

querystring serializer

MyClass.prototype.serialize = function(obj, prefix) {
var _temp = [];
for(var p in obj) {
    if(typeof obj[p] == "function") continue;
    var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
    _temp.push(typeof v == "object" ? 
        this.serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
return _temp.join("&").replace(/\s/gi,'');
}
// usage:
myObj = new MyClass();
myObj.serialize({k1:"v1", "k2":{k2_1:"v2_1"}});