Anonymous constructor functions to work as classes in JavaScript

by Jagadish Pulakhandam on 8/15/2012 7:42:21 AM
Rated 0 from 0 votes
Brief: Demonstrates on using anonymous constructor functions to define "class" like structures.
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 define a class like structure using anonymous function constructors in JavaScript
  • How to instantiate an object using anonymous function constructor 
  • How to access object properties using "with" construct
  • How to send object as a parameter to a function
NOTE:  JavaScript does not have built-in support for "pure" classes.  We can achieve similar to "classes" using "function constructors"

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: using anonymous constructors functions to define objects
08.        //      using "with" keyword with an object
09. 
10.        //defining a (class/anonymous function) constructor accepting 4 parameters
11.        var Emp = function (vEmpno, vEname, vSal, vDeptno) {
12.            this.Empno = vEmpno;
13.            this.Ename = vEname;
14.            this.Sal = vSal;
15.            this.Deptno = vDeptno;
16.        };
17. 
18.        //function to display objects
19.        function ShowEmp(o) {
20.            //accessing members of an object
21.            //alert(o.Empno.toString() + ", " +
22.            //        o.Ename + ", " +
23.            //        o.Sal.toString() + ", " +
24.            //        o.Deptno.toString()
25.            //);
26. 
27.            //another way of accessing object members using "with" construct
28.            with (o) {
29.                alert(Empno.toString() + ", " +
30.                        Ename + ", " +
31.                        Sal.toString() + ", " +
32.                        Deptno.toString()
33.                );
34.            }
35.        }
36. 
37.        //creating (multiple) objects
38.        var oEmp1 = new Emp(1001, "Jag", 5400, 10);
39.        var oEmp2 = new Emp(1002, "Chat", 6400, 20);
40.        var oEmp3 = new Emp(1003, "Win", 7800, 10);
41.        var oEmp4 = new Emp(1004, "Dhan", 9000, 10);
42. 
43.        //display all objects
44.        ShowEmp(oEmp1);
45.        ShowEmp(oEmp2);
46.        ShowEmp(oEmp3);
47.        ShowEmp(oEmp4);
48. 
49.    </script>
50.</head>
51.<body>
52. 
53.</body>
54.</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