Posted By

noah on 07/08/07


Tagged

object toggle library property oo utilities nested


Versions (?)

Toggle value of nested property


 / Published in: JavaScript
 

I feel like I once found an elegant way of setting nested properties without using eval(). However i can't find that code, and atm this is the best I can do.

  1. //toggle object property value
  2. //only works if the object is in the global scope :(
  3. function tog(propertyStr, value, defaultValue) {
  4. var property = propertyStr.split('.');
  5. /* var propAddr = '[' + propertyStr.join('][') + ']'; */
  6. var propAddrStr = property.shift() + '["' + property.join('"]["') + '"]';
  7. var propValue = eval(propAddrStr);
  8. var setPropStr = propAddrStr + '= "' + (propValue == value ? defaultValue : value ) + '"';
  9. eval(setPropStr);
  10. }

Report this snippet  

You need to login to post a comment.