Skip to content

Instantly share code, notes, and snippets.

@kolber
Created June 2, 2010 02:11
Show Gist options
  • Save kolber/421819 to your computer and use it in GitHub Desktop.
Save kolber/421819 to your computer and use it in GitHub Desktop.
<script>
/*
Transform a standard js object into a callable function with all the same properties as the original object.
*/
/* Variables */
var o = { test: 'data1', test2: 'data2' }
var func_body = function() { alert('hi'); }
/* Construction */
var o = (function(obj, function_body) {
var inner = function_body;
for(prop in obj) {
if(obj.hasOwnProperty(prop))
inner[prop] = obj[prop];
}
return inner;
}).apply(this, [o, func_body]);
/* Tests */
console.log(o.test);
console.log(o.test2);
o();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment