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"}});
Written by Luca Simonetti
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Serialize
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#