Exforsys.com
 
Home Tutorials Microsoft AJAX
 

Microsoft AJAX Library - Creating Object Members on the Fly

 

Microsoft AJAX Library - Creating Object Members on the Fly

Page 1 of 2

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.



Sample Code
  1. // create a Date object
  2. var myDate = new Date()
  3. // create a new member named ImADate in the oDate object
  4. myDate.ImADate = "I'm a Date!"
  5. // display the value of oDate.ImADate
  6. document.write(myDate.ImADate)
Copyright exforsys.com



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.


Sample Code
  1. function Table (rows, columns)
  2. {
  3. // save parameter values to local variables
  4. var _rows = rows
  5. var _columns = columns
  6. // return the number of table cells
  7. this.getCellCount = function()
  8. {
  9. return _rows * _columns
  10. }
  11. }
Copyright exforsys.com



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.


Sample Code
  1. // create a Table object
  2. var t = new Table(3,5)
  3. // display object field values
  4. document.write("Your table has " + t._rows + " rows" +
  5. " and " + t._columns + " columns
  6. ")
  7. // call object function
  8. document.write("The table has " + t.getCellCount() + " cells
  9. ")
Copyright exforsys.com




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


Read Next: Microsoft AJAX Library - JavaScript Execution Context



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape