Thursday, November 15, 2007

Creating classes

JavaScript functions are used to represent class objects in the type system. The AJAX Library follows the pattern of declaring a function as the class constructor. JavaScript allows you to modify the prototype of the function directly, which is how the AJAX Library creates class members. The class must then be registered so that it can participate in the semantics of the type system.

varibale scope: the local memebers are accessed with a prefix of 'this', the script engine can then scope the lookup to the type and avoid searching any containing scopes. If you do not use this to indicate that the reference is local to the type. you will end up creating objects in the global scopt and see errors that can be confusing and time-consuming to track down.


Type.registerNamespace('Costco.ASPAJAX.Address');
Costco.ASPAJAX.Address= function (name, email)
{
this._name = name;
this._email = email;
}
Costco.ASPAJAX.Address.prototype =
{
get_name: function(){
return this._name;
},
get_email: function()
{
return this._email;
}
}
Costco.ASPAJAX.Address.registerClass('Costco.ASPAJAX.Address')
var address = new Costco.ASPAJAX.Address('you name','youname@costco.com');
alert(address.get_name());

No comments: