/ 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.
Expand |
Embed | Plain Text
//toggle object property value //only works if the object is in the global scope :( function tog(propertyStr, value, defaultValue) { var property = propertyStr.split('.'); /* var propAddr = '[' + propertyStr.join('][') + ']'; */ var propAddrStr = property.shift() + '["' + property.join('"]["') + '"]'; var propValue = eval(propAddrStr); var setPropStr = propAddrStr + '= "' + (propValue == value ? defaultValue : value ) + '"'; eval(setPropStr); }
You need to login to post a comment.
