Declare, Define and Initialize objects with Properties in JavaScript

by Jagadish Pulakhandam on 8/10/2012 11:15:30 AM
Rated 0 from 0 votes
Brief: Demonstrates on declaring, defining and initializing objects with properties in various ways.
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 declare/define an object in JavaScript
  • Various ways to define and initialize objects in JavaScript
  • How to define (or add) properties to an object in JavaScript
  • How to access properties of an object in JavaScript
    • using object notation
    • using array notation

Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script>
07.        //DEMO: declaring/defining/initializing objects with properties
08. 
09.        //method 1
10.        var oEmp1 = Object();
11.        oEmp1.Empno = 1001; //property name is "Empno" and the value is 1001
12.        oEmp1.Ename = "Jag";
13.        oEmp1.Sal = 5400;
14.        oEmp1.Deptno = 10;
15. 
16.        alert(oEmp1.Empno.toString() + ", " +
17.                oEmp1.Ename + ", " +
18.                oEmp1.Sal.toString() + ", " +
19.                oEmp1.Deptno.toString()
20.        );
21. 
22. 
23.        //method 2
24.        var oEmp2 = {}; //empty braces
25.        oEmp2.Empno = 1002;
26.        oEmp2.Ename = "Jag";
27.        oEmp2.Sal = 5400;
28.        oEmp2.Deptno = 10;
29. 
30.        alert(oEmp2.Empno.toString() + ", " +
31.                oEmp2.Ename + ", " +
32.                oEmp2.Sal.toString() + ", " +
33.                oEmp2.Deptno.toString()
34.        );
35. 
36. 
37.        //method 3 (using key/value pair notation to define properties/values)
38.        var oEmp3 = {
39.            Empno:1003,
40.            Ename:"Jag",
41.            Sal:5400,
42.            Deptno:10
43.        };
44.        //keys can be optionally placed in quotes/double quotes (if necessary)
45. 
46.        alert(oEmp3.Empno.toString() + ", " +
47.                oEmp3.Ename + ", " +
48.                oEmp3.Sal.toString() + ", " +
49.                oEmp3.Deptno.toString()
50.        );
51. 
52.        //accessing object properties using array notation
53.        alert(oEmp3["Empno"].toString() + ", " +
54.                oEmp3["Ename"] + ", " +
55.                oEmp3["Sal"].toString() + ", " +
56.                oEmp3["Deptno"].toString()
57.        );
58. 
59.        //accessing object properties using a loop
60.        for (p in oEmp3) {
61.            alert(oEmp3[p]);
62.        }
63. 
64.    </script>
65.</head>
66.<body>
67. 
68.</body>
69.</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

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following