How to create nested objects using constructor functions in JavaScript

by Jagadish Pulakhandam on 8/30/2012 1:42:11 PM
Rated 0 from 0 votes
Brief: Demonstrates on creating structures (classes) which contain nested objects/properties
Posted to: Advanced JavaScript Programming/Development tutorials/videos- Object Oriented Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached Source Code/Video demonstrates the following:
  • How to create classes with nested objects using constructor functions in JavaScript
  • How to instantiate classes containing nested objects and access their properties.
Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script type="text/javascript">
07.        //DEMO: nested objects in constructor functions
08. 
09.        var Emp = function (vName, vSal, vDeptno, vDname) {
10.            this.Ename = vName;
11.            this.Sal = vSal;
12.             
13.            //nested object
14.            this.Dept = {
15.                Deptno:vDeptno,
16.                Dname:vDname
17.            };
18.             
19.        };
20. 
21.        //create object
22.        var oEmp = new Emp("Jag", 5400, 10, "IT");
23.        //accessing property of nested object from parent object
24.        alert(oEmp.Ename + " works in " + oEmp.Dept.Dname);
25.    </script>
26.</head>
27.<body>
28. 
29.</body>
30.</html>



Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following