|
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.
// 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);
%2F%2F%20create%20a%20Date%20object%20%0A%0D%0Avar%20myDate%20%3D%20new%20Date%28%29%3B%20%0A%0D%0A%2F%2F%20create%20a%20new%20member%20named%20ImADate%20in%20the%20oDate%20object%20%0A%0D%0AmyDate.ImADate%20%3D%20%22I%27m%20a%20Date%21%22%3B%20%0A%0D%0A%2F%2F%20display%20the%20value%20of%20oDate.ImADate%20%0A%0D%0Adocument.write%28myDate.ImADate%29%3B
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; }; }
function%20Table%20%28rows%2C%20columns%29%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20save%20parameter%20values%20to%20local%20variables%20%0A%0D%0Avar%20_rows%20%3D%20rows%3B%20%0A%0D%0Avar%20_columns%20%3D%20columns%3B%20%0A%0D%0A%2F%2F%20return%20the%20number%20of%20table%20cells%20%0A%0D%0Athis.getCellCount%20%3D%20function%28%29%20%0A%0D%0A%7B%20%0A%0D%0Areturn%20_rows%20%2A%20_columns%3B%20%0A%0D%0A%7D%3B%20%0A%0D%0A%7D
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 ");
%2F%2F%20create%20a%20Table%20object%20%0A%0D%0Avar%20t%20%3D%20new%20Table%283%2C5%29%3B%20%0A%0D%0A%2F%2F%20display%20object%20field%20values%20%0A%0D%0Adocument.write%28%22Your%20table%20has%20%22%20%2B%20t._rows%20%2B%20%22%20rows%22%20%2B%20%0A%0D%0A%22%20and%20%22%20%2B%20t._columns%20%2B%20%22%20columns%20%0A%0D%0A%22%29%3B%20%0A%0D%0A%2F%2F%20call%20object%20function%20%0A%0D%0Adocument.write%28%22The%20table%20has%20%22%20%2B%20t.getCellCount%28%29%20%2B%20%22%20cells%20%0A%0D%0A%22%29%3B

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.
|