Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [440]

By Root 5780 0
today.getDate();

var thisMonth = today.getMonth();

var thisYear = today.getFullYear();

var age = thisYear-this.BirthDate.getFullYear()-1;

if (thisMonth > this.BirthDate.getMonth())

age = age +1;

else

if (thisMonth == this.BirthDate.getMonth() &&

thisDay >= this.BirthDate.getDate())

age = age +1;

return age;

}

What we have is an object modeled after a person; we don’t have a Person object. A possible way to define the layout of a type is to create a new, all-encompassing function that exposes just the members we like. In addition, in JavaScript all intrinsic objects have a read-only property named prototype. You can use the prototype property to provide a base set of functionality shared by any new instance of an object of that type. These two are the mechanisms to leverage for using OOP in JavaScript.

Using Closures


A closure is a general concept of programming languages. Applied to JavaScript, a closure is a function that can have variables and methods defined together within the same context. In this way, the outermost (anonymous or named) function “closes” the expression. Here’s an example of the closure model for a function that represents a Person type:

var Person = function(name, lastname, birthdate)

{

this.Name = name;

this.LastName = lastname;

this.BirthDate = birthdate;

this.getAge = function() {

var today = new Date();

var thisDay = today.getDate();

var thisMonth = today.getMonth();

var thisYear = today.getFullYear();

var age = thisYear-this.BirthDate.getFullYear()-1;

if (thisMonth > this.BirthDate.getMonth())

age = age +1;

else

if (thisMonth == this.BirthDate.getMonth() &&

thisDay >= this.BirthDate.getDate())

age = age +1;

return age;

}

}

As you can see, the closure is nothing more than the constructor of the pseudo-class. In a closure model, the constructor contains the member declarations and members are truly encapsulated and private to the class. In addition, members are instance based, which increases the memory used by the class. Here’s how you use the object:

var p = new Person("Dino", "Esposito", new Date( ... );

alert(p.Name + " is " + p.getAge());

The closure model gives full encapsulation, but nothing more. To compose objects, you can only resort to aggregation.

Using Prototypes


The prototype model entails that you define the public structure of the class through the JavaScript prototype object. The following code sample shows how to rewrite the preceding Person class to avoid a closure:

// Pseudo constructor

var Person = function(name, lastname, birthdate)

{

this.initialize(name, lastname, birthdate);

}

// Members

Person.prototype.initialize(name, lastname, birthdate)

{

this.Name = name;

this.LastName = lastname;

this.BirthDate = birthdate;

}

Person.prototype.getAge = function()

{

var today = new Date();

var thisDay = today.getDate();

var thisMonth = today.getMonth();

var thisYear = today.getFullYear();

var age = thisYear-this.BirthDate.getFullYear()-1;

if (thisMonth > this.BirthDate.getMonth())

age = age +1;

else

if (thisMonth == this.BirthDate.getMonth() &&

thisDay >= this.BirthDate.getDate())

age = age +1;

return age;

}

In the prototype model, the constructor and members are clearly separated and a constructor is always required. As for private members, you just don’t have them. The var keyword that would keep them local in a closure doesn’t apply in the prototype model. So you can define getter/setter for what you intend to be properties, but the backing field will remain accessible from the outside, anyway. You can resort to some internal (and documented) convention, such as prefixing with an underscore the name of members you intend as private. That’s just a convention, however.

By using the prototype feature, you can achieve inheritance by simply setting the prototype of a derived object to an instance of the “parent” object:

Developer = function Developer(name, lastname, birthdate)

{

this.initialize(name, lastname, birthdate);

}

Developer.prototype = new Person();

Note that you always need to

Return Main Page Previous Page Next Page

®Online Book Reader