Skip to content

Instantly share code, notes, and snippets.

@mrjoelkemp
Created September 11, 2013 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjoelkemp/6524775 to your computer and use it in GitHub Desktop.
Save mrjoelkemp/6524775 to your computer and use it in GitHub Desktop.
For every function on a given object, set up the function to auto-parse stringified arguments. Use case: In Flash to Javascript communication, this avoids the need to manually parse for every JS api endpoint.
function parseFunctionArguments (obj) {
for (var method in obj) {
if (obj.hasOwnProperty(method) && typeof obj[method] === 'function') {
(function (method) {
var func = obj[method];
obj[method] = function () {
// Try to convert each string arg into an object literal
var converted = [].map.call(arguments, function (v) {
if (typeof v === 'string') {
try {
v = JSON.parse(v);
} catch (e) {}
}
return v;
});
func.apply(obj, converted);
};
})(method);
}
}
}
a = {
foo: function () {
console.log('foo arguments', arguments);
},
bar: function () {
console.log('bar arguments', arguments);
}
};
parseFunctionArguments(a);
a.foo('{"f": 1}', 1, 2);
a.bar('{"b": 2}', 3, 4);
// Output
foo arguments [Object, 1, 2]
bar arguments [Object, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment