alt
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow Microsoft AJAX arrow Microsoft AJAX Library - Creating Object Members on the Fly
Site Search


Microsoft AJAX Library - Creating Object Members on the Fly
Article Index
Microsoft AJAX Library - Creating Object Members on the Fly
Prototypes

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.

  1. // create a Date object
  2.  
  3. var myDate = new Date();
  4.  
  5. // create a new member named ImADate in the oDate object
  6.  
  7. myDate.ImADate = "I'm a Date!";
  8.  
  9. // display the value of oDate.ImADate
  10.  
  11. 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.

  1. function Table (rows, columns)
  2.  
  3. {
  4.  
  5. // save parameter values to local variables
  6.  
  7. var _rows = rows;
  8.  
  9. var _columns = columns;
  10.  
  11. // return the number of table cells
  12.  
  13. this.getCellCount = function()
  14.  
  15. {
  16.  
  17. return _rows * _columns;
  18.  
  19. };
  20.  
  21. }
 

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.

  1. // create a Table object
  2.  
  3. var t = new Table(3,5);
  4.  
  5. // display object field values
  6.  
  7. document.write("Your table has " + t._rows + " rows" +
  8.  
  9. " and " + t._columns + " columns
  10. ");
  11.  
  12. // call object function
  13.  
  14. document.write("The table has " + t.getCellCount() + " cells
  15. ");
 

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.



 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape