Created
January 23, 2013 10:50
-
-
Save lightjs/4604334 to your computer and use it in GitHub Desktop.
Turn a json into a full object, with getter / setter for all values and init function for new getter / setter.
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
var jsonize = function(obj) { | |
if( typeof obj == 'object' && !(obj[k] instanceof Array) && Object.keys(obj).length > 0 ) { | |
for( var k in obj ) { | |
if( obj[k] instanceof Array ) for( var k2 in obj[k] ) obj[k][k2] = jsonize(obj[k][k2]); | |
else if( typeof obj[k] == 'object' && !(obj[k] instanceof Array) && Object.keys(obj[k]).length > 0 ) jsonize(obj[k]); | |
obj['_'+k] = obj[k]; | |
obj[k] = (function(n) { | |
return function(_) { | |
if(!arguments.length) return obj['_'+n]; | |
obj['_'+n] = _; | |
return obj; | |
} | |
})(k); | |
} | |
obj['init'] = function(n) { | |
obj[n] = function(_) { | |
if(!arguments.length) return obj['_'+n]; | |
obj['_'+n] = _; | |
return obj; | |
} | |
return obj; | |
} | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment