Exforsys.com
 
Home Tutorials Microsoft AJAX
 

Microsoft AJAX Library - C# and JavaScript Classes

 

Production-quality C# implementation

Page 2 of 2


Production-quality C# implementation

In a production-quality C# implementation you may want to implement rows and columns as properties with get and set assessors, rather than public fields. That implementation, however, would make its JavaScript version more complicated than necessary for the purposes of our examples.



The Table class can be easily implemented in JavaScript as shown in the following code snippet, and it would resemble very much its C# version:


Sample Code
  1.  function Table (rows, columns)
  2. {
  3. // "constructor"
  4. this.rows = rows
  5. this.columns = columns
  6. // getCellCount "method"
  7. this.getCellCount = function()
  8. {
  9. return this.rows * this.columns
  10. }
  11. }
Copyright exforsys.com



After having declared the object, we can instantiate it by using the new operator and use its properties and methods:


Sample Code
  1. var t = new Table(3,5)
  2. var cellCount = t.getCellCount()
Copyright exforsys.com



There are a few subtle points you need to notice regarding the JavaScript implementation of Table:


You don't declare public members explicitly. You simply need to reference them using this, and assign some value to them; from that point on, they're both declared and defined.


JavaScript allows you to implement most of the design specifications defined in class diagrams, but the implementation can't reflect the specification as accurately as a C# implementation can. For example, the line Table (int rows, int columns) in the diagram in Figure 3-3 refers to the constructor of the class. In JavaScript, as you know, classes as implemented using functions neither have real constructors, nor support specifying data types for their parameters.


When objects are created, each object has its own set of data—to maintain its own state. However, C# and JavaScript are different in that in JavaScript functions are first-class objects. In C#, the "state" is made of the object's fields. The object functionality, as defined by its methods, is the same for all objects of the same type. For example, if you create many objects of the type Table in C#, each object will have its own set of rows and columns, but internally they all use the same copy of the getCellCount() method. In JavaScript, however, functions are treated like any other variable. In other words, creating a new Table object in JavaScript will result not only in creating a new set of rows and columns values, but also in a new copy of the getCellCount() method. Usually, you don't need (or want) this behavior.



The last mentioned problem is commonly referred to as a "memory leak", although technically it's just inefficient JavaScript object design. When we design our JavaScript "classes" as we do in typical OOP languages, we don't need each class to create its own set of methods. It's only state (fields) that need to be individual, and not methods' code. The good news is that JavaScript has a neat trick that we can use to avoid replicating the inner function code for each object we create: referencing external functions.



Referencing External Functions

In stead of defining member functions ("methods") inside the main function ("class") as shown earlier, you can make references to functions defined outside your main function, like this:



Sample Code
  1. function Table (rows, columns)
  2. {
  3. // "constructor"
  4. this.rows = rows
  5. this.columns = columns
  6. // getCellCount "method"
  7. this.getCellCount = getCellCount
  8. }
  9. // returns the number of rows multiplied by the number of columns
  10. function getCellCount()
  11. {
  12. return this.rows * this.columns
  13. }
Copyright exforsys.com



Now, all your Table objects will share the same instance of getCellCount(), which is what you will usually want.




First Page: Microsoft AJAX Library - C# and JavaScript Classes


Read Next: Microsoft AJAX Library - Associative Arrays



 

 

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 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape