Created
September 11, 2013 14:54
-
-
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.
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 characters
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