Nov
29
JavaScript - Object Creation with JSON
November 29, 2007 |
Using JSON for object creation is very similar to Object Creation using array. The difference is that we use curly braces in JSON, and square brakets in array, (you can go to one of my previous post about using array for object creation). Functions can be defined inline or can reference external functions(an example of inline and external function can be found from one of my previous post). You can nest object definitions as much as you like in JSON to create a hierarchy of objects. Below is an example of object creation using JSON:
function sayLoudly() {
alert(this.firstName.toUpperCase());
}
var newObject = {
firstName : "James",
sayName : function() { alert(this.firstName); },
sayLoudly : sayLoudly,
LastName : {
lastName : "Bond",
sayName : function() { alert(this.lastName); }
}
};
To call the function below, you can get the James Bond’s last name.
newObject.LastName.sayName();
Below is the link to the demo page, enjoy!
http://www.lab.highub.com/javascript/object-creation-json.html
Similar Posts
- None Found


































