Created
June 2, 2010 02:11
-
-
Save kolber/421819 to your computer and use it in GitHub Desktop.
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
<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