Last active
August 29, 2015 14:19
-
-
Save joselee/ec0bbc2403a2dfb8d3e2 to your computer and use it in GitHub Desktop.
Recursive proto
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 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