Skip to content

Instantly share code, notes, and snippets.

@joselee
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save joselee/ec0bbc2403a2dfb8d3e2 to your computer and use it in GitHub Desktop.

Select an option

Save joselee/ec0bbc2403a2dfb8d3e2 to your computer and use it in GitHub Desktop.
Recursive proto
var myClass = function(json){
var self = this;
self.myMethod = function(){
console.log("Calling my method")
};
for(var key in json){
if(typeof json[key] === "object"){
self[key] = new myClass(self[key]);
} else {
self[key] = json[key];
}
}
};
var jsonObject = {a:"1", b:{b1:"2"}};
// All objects and sub-objects have myMethod()
var classedJson = new myClass(jsonObject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment