Introduction
( Patience please i am write the article. Please, patience! )
When we study the opportunity from programming JavaScript with the best techniques from OOP, we find several troubleshotings. This is co
http://www.codeproject.com/aspnet/JsOOP1.asp
http://www.codeproject.com/aspnet/JsOOP2.asp
Namespace from Example.
var Article </CODE>= {};
Ok, the idea beginin creating Class Base. That clas will implement Getter and Setter technique. Note wath _ (for us, the private attribute container) will be inherited for target Class. When we inherite from a Class, we hoped what the attributes will be inherited.
Article.Base = function()
{
var <CODE>_</CODE> = {};
Later construct the private method (
_getAttributes), protected, and only will can call from:
- HasProperty. (public method for to prove the existence of an property)
- GetProperties (static method for construct inherits)
- Add (for Custom class)
In methods outside from object constructed, _getAttributes is not valid, if the programmer try use will throw exception with caller code. That last feature will can usefull from fix code.
this._getAttributes = function()
{
switch( this._getAttributes.caller )
{
case this.HasProperty:
case this.Add:
case Article.Extends:
return _;
break;
default:
throw "Invalid Method Caller " + "\n" + this._getAttributes.caller;
}
}
This public method (protected into the Constructor), verify the existence from a property.
this.HasProperty = function( property </CODE></CODE></CODE></CODE></CODE></CODE></CODE>)
{
return ( property in this._getAttributes() );
}
The internal method ._get, only call from public method Get. When we Encapsulate this method, the attributes is protected, the Class Inherits from class Base, will can rewrite public Get.
<BR>
this._get = function( property )
{
var caller </CODE>= this._get.caller;
if( caller</CODE> != this.Get ) throw "Invalid Method Caller" + "\n" + caller;
if( !this.HasProperty(property) ) throw "Property Undefined" + " -> " + property + ": \n" + caller;
return _[property];
}
The same case from _get, the _set method is protected, and only is public the Set method. The public Set will can overwrite for other class what inherits from Base. Notice that the method throw an exception if violates the rule.
<BR>
this._set = function( property, value )
{
var caller </CODE>= this._get.caller;
if( caller</CODE> != this.Set ) throw "Invalid Method Caller" + "\n" + caller;
if( !this.HasProperty(property) ) throw "Property Undefined" + " -> " + property + " \n" + caller;
_[property] = value;
}
}
The rest from public methods. All can be overriden for other class (excepting methods
_get,
_set and
_getAttributes, obvious )
About the public Get method
Article.Base.prototype.Get = function( property )
{
return this._get( property );
}
About the public
Set method
Article.Base.prototype.Set = function( property, value )
{
this._set( property, value );
}
Article.Extends
Important Static method. This to do inherits from Base Class. Copy internal attributes for the container in the Target Class. (The explanation, patience, i have write in english. )
<BR>
Article.Extends = function()
{
var obj = arguments[0];
var caller = Article.Extends.caller;
if( typeof(obj) != "object" ) throw "Hieretic Target Undefined";
switch( arguments.length )
{
case 0:
throw "Error, target reference is necessary.";
break;
case 1:
arguments[1] = Article.Base;
case 2:
var parentClass = arguments[1];
parentClass.apply( obj, new Array() );
for( var x in parentClass.prototype ) obj[x] = parentClass.prototype[x];
for( var x in caller.prototype ) obj[x] = caller.prototype[x];
return obj._getAttributes();
break;
}
}
Example:
Simple inhertis from Base Class. View the attribute container, that is protected.
<BR>function TestClass()
{
var Attributes </CODE>= Article.Extends( this );
<CODE>Attributes</CODE>.Id = "123456";
<CODE>Attributes</CODE>.Name = "Hello World";
}
TestClass.prototype.Get = function ( <CODE>property </CODE>)
{
return this._get(<CODE>property</CODE>) + " modifiqued";
}
var myTestClass = new TestClass();
var name = myTestClass.Get( "Name" );
<CODE>alert</CODE>( name );
Simple inhertis from
TestClass. View the attribute container, that is protected. The Get property is overloaded.
<BR> function TestClassReady()
{
var Attributes </CODE>= Article.Extends( this );
<CODE>Attributes </CODE>= Article.Extends( this, TestClass );
<CODE>Attributes</CODE>.Ready = true;
}
TestClassReady.prototype.Get = function ( property )
{
return this._get(property) + " ready";
}
var myTestClassReady = new TestClassReady();
var name = myTestClassReady.Get( "Name" );
<CODE>alert</CODE>( name );
var ready = myTestClassReady.Get( "Ready" );
<CODE>alert</CODE>( ready );
Later explain all.. in my bad english :-(
( Patience please i am write the article. Please, patience! )
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.