Sponsored Links
Microsoft AJAX Tutorials
- Microsoft AJAX Library Essentials
- Concepts of Object-Oriented Programming
- Microsoft AJAX Library - Object-Oriented JavaScript
- Microsoft AJAX Library - JavaScript Functions
- Microsoft AJAX Library - Functions as Variables
- Microsoft AJAX Library - Anonymous Functions
- Microsoft AJAX Library - JavaScript Classes
- Microsoft AJAX Library - C# and JavaScript Classes
- Microsoft AJAX Library - Associative Arrays
- Microsoft AJAX Library - Creating Object Members on the Fly
- Microsoft AJAX Library - JavaScript Execution Context
- Microsoft AJAX Library - Inheritance using Closures
- Microsoft AJAX Library - Inheritance using Prototypes
- Microsoft AJAX Library - Introducing JSON
Tutorials
Microsoft AJAXMicrosoft AJAX Library - Creating Object Members on the Fly
Microsoft AJAX Library - Creating Object Members on the Fly
Creating Object Members on the Fly
One major difference between OOP in C# and ASP.NET, and OOP in JavaScript, is that JavaScript allows creating object members "on the fly". This is true for objects and classes that you create yourself and also for JavaScript's own objects and types as well. Here's an example where we add a field named ImADate to a JavaScript Date object.
- // create a Date object
- var myDate = new Date()
- // create a new member named ImADate in the oDate object
- myDate.ImADate = "I'm a Date!"
- // display the value of oDate.ImADate
- document.write(myDate.ImADate)
A typical OOP language such as C#, VB.NET, or Java, doesn't allow you to create members on the fly, like JavaScript does. Instead, each member must be defined formally in the definition of the class.
Private Members
JavaScript doesn't support the notion of private members as C# does, but you can simulate the functionality by using variables inside the function. Variables are declared with the var keyword or are received as function parameters. They aren't accessed using this, and they aren't accessible through function instances, thus acting like private members. Variables can, however, be accessed by closure functions.
If you want to test this, modify the Table function as shown below.
- function Table (rows, columns)
- {
- // save parameter values to local variables
- var _rows = rows
- var _columns = columns
- // return the number of table cells
- this.getCellCount = function()
- {
- return _rows * _columns
- }
- }
This time we persist the values received as parameters as local variables named _rows and _columns. Note they aren't referred to using this any more. Local variables names don't need to start with an underscore, but this is a useful naming convention that specifies they are meant to be used as private members. You can make a short test that the "private" members can't be accessed from outside the function, and that getCellCount() still works, using code such as the following. The results are shown in Figure 3-5.
- // create a Table object
- var t = new Table(3,5)
- // display object field values
- document.write("Your table has " + t._rows + " rows" +
- " and " + t._columns + " columns
- ")
- // call object function
- document.write("The table has " + t.getCellCount() + " cells
- ")

Figure 3-5. JavaScript example demonstrating "private" members
This exercise reveals that _rows and _columns aren't accessible from outside the function's scope. Their values read undefined because there are no fields named _rows and _columns in the Table function. The getCellCount() function, on the other hand, can read _rows and _columns as variables because they are in the same closure. As you can see, although the implementation and behavior are somewhat different than in C#, you still have a way of defining internal (private) members inside a function.
Next Page: Prototypes
Comments
Sponsored Links
