viernes, 6 de agosto de 2010

JavaScript Class using JSON prototype


/// <summary>
/// Class that represents a customer of our store.
/// The constructor receives the name of the customer
/// </summary>
/// <param name="firstName"> First name of the customer.
/// <param name="lastName"> Last name of the customer.
function Customer(firstName, lastName)
{
// Properties
this.FirstName = firstName;
this.LastName = lastName;
}

Customer.prototype = {

/// <summary>
/// Returns the first name of the customer
/// </summary>
GetFirstName: function()
{
return this.FirstName;
},

/// <summary>
/// Returns the last name of the customer
/// </summary>
GetLastName: function()
{
return this.LastName;
},
...
}


Usage:


var aCustomer = new Customer("Pablo","Bustos");
alert("Hello " + aCustomer.GetFirstName());

Good programmers

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Refactoring: Improving the Design of Existing Code by Martin Fowler