Revision: 15971
Updated Code
at July 22, 2009 09:35 by straps
Updated Code
/** * Usage: * var o={a:1, b:'!! 1+1', c:'!! alert(\'c\')'}; * evalObj(o); * //o.a===1 * //o.b===2 * //o.c() will display an alert * * Updated to keep the scope, usage: * var o={a:1, b:'!! this.a+1', c:'!! alert(this.b+1)'}; * evalObj.call(o,o); * //o.a===1 * //o.b===2 * //o.c() will display an alert with 3 * */ function evalObj(o,rec){ if (rec === undefined) { rec=true; } for (var name in o){ var elem=o[name]; if (elem){ var telem=typeof elem; if (telem==='string' && /^!!/.test(elem)){ o[name]=new Function('return '+elem.substr(2)).call(this); }else if (rec && telem==='object'){ evalObj.call(this,elem,rec); } } } return o; }
Revision: 15970
Updated Code
at July 22, 2009 03:26 by straps
Updated Code
/** * Usage: * var o={a:1, b:'!! 1+1', c:'!! alert(\'c\')'}; * evalObj(o); * //o.a===1 * //o.b===2 * //o.c() will display an alert */ function evalObj(o,rec){ if (rec === undefined) { rec=true; } for (var name in o){ var elem=o[name]; if (elem){ var telem=typeof elem; if (telem==='string' && /^!!/.test(elem)){ o[name]=new Function('return '+elem.substr(2))(); }else if (rec && telem==='object'){ evalObj(elem); } } } return o; }
Revision: 15969
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 22, 2009 03:25 by straps
Initial Code
/** * Usage: * var o={a:1, b:'!! 1+1', c:'!! alert(\'c\')'}; * evalObj(o); * //o.a===1 * //o.b===2 * //o.c() will display an alert */ function evalObj(o,rec){ if (rec === undefined) { rec=true; } for (var name in o){ var elem=o[name]; if (!empty(elem)){ var telem=typeof elem; if (telem==='string' && /^!!/.test(elem)){ o[name]=new Function('return '+elem.substr(2))(); }else if (rec && telem==='object'){ evalObj(elem); } } } return o; }
Initial URL
http://straps.tumblr.com/post/146642767/evalobj
Initial Description
Runtime Object Evaluator; search for string object properties that start with !! and eval the content. Very useful if you want to load JSON from file and evaluate its fields on effective usage or access global variables at runtime. [Follow me on Twitter for updates](http://twitter.com/fstraps/ "fstraps on Twitter")
Initial Title
Object Runtime Fields Evaluator
Initial Tags
object, function
Initial Language
JavaScript