Click here to Skip to main content
15,901,283 members
Articles / Programming Languages / Javascript
Article

Inherits private variables and use compact Get and Set technique with Javascript

Rate me:
Please Sign up or sign in to vote.
1.89/5 (3 votes)
10 Oct 20062 min read 26.9K   6   7
Using Getter and Setter, inherits private variables beetwen class, only Get and Set from Javascript
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. 

 
/**
 * Class Base
 */
Article.Base = function()
{
    // Private Attribute Container</CODE>
    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.

   

    /**
    * Get  Attributes from Class
    */
    this._getAttributes = function()
    {
      switch( this._getAttributes.caller )  
      {
        case this.HasProperty:
        case this.Add:
	case Article.Extends:
            return _;  /*  return the attributes */
            break;
        default:
            throw "Invalid Method Caller " + "\n" + this._getAttributes.caller;
      }
    }

   

   This public method (protected into the Constructor), verify the existence from a property.

JavaScript
   /**
    * Verify if exist the property
    * @parameter property {string} Name from 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.

JavaScript
    <BR>     /**
     * Get value from property
     *
     * @parameter string Name from property
     * @return {Object} Value from property
     */
    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.

JavaScript
<BR>   /**
     *  Set new value for property
     *
     *  @parameter {string} Name from property
     *  @value {object} The new value from property
     *  @return {nothing}
     */
    this._set = function( property, value )
    {
       var caller </CODE>= this._get.caller;  // who is 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

/**
 * It allows the use of the protected method _get
 * parameter {string} Name from Property


 */
Article.Base.prototype.Get = function( property )
{
    return this._get( property );
}
About the public Set method
/**
 * It allows the use of the protected method _set
 * parameter {string} Name from Property
 * parameter {object} value New value for the Property

 */
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. )
JavaScript
 <BR>/** 
* Create inhertis from Class Base and Other Class 
*/ 
Article.Extends = function()
{         
   var obj    = arguments[0];
   var caller = Article.Extends.caller;
   if( typeof(obj) != "object" ) throw "Hieretic Target Undefined";
   /*
    *  The reference this is important, how minimal argument 
    *  Inherits from Parent class
    *  Clone de prototype  attributes from Parent Clas
    *  Set prototype attributes again (override de properties)
    *  always return attributes, that is important
    *  
   */
   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.

JavaScript
 <BR>function TestClass()
{
      /*  Inherits from Base Class */
      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.

JavaScript
<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! )

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Venezuela Venezuela
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWrite it off line first Pin
Colin Angus Mackay10-Oct-06 23:16
Colin Angus Mackay10-Oct-06 23:16 
GeneralRe: Write it off line first Pin
Roger Zavala11-Oct-06 4:51
Roger Zavala11-Oct-06 4:51 
QuestionHow does _getAttributes work? Pin
araud10-Oct-06 0:54
araud10-Oct-06 0:54 
AnswerRe: How does _getAttributes work? [modified] Pin
Roger Zavala10-Oct-06 7:55
Roger Zavala10-Oct-06 7:55 
GeneralRe: How does _getAttributes work? Pin
araud10-Oct-06 20:01
araud10-Oct-06 20:01 
Sorry, I dindn't note you are at work. Now article is looking quite better. English is not my mother language too, I'm russian. This research have a great value. JS is used not only in DHTML but even in games and commercial solutions. Ability to have such features as inheritance and private members is quite needful. This would be a great work to make JS++.js file containing all tricks to enable adult language's features. As I can see there is no problem to draw near inheritance and other stuff to c++ syntax. For example:

function CMyClass{
private();
PrivateFunction = function ()...
var PrivateMember = ...
protected();
ProtectedFunction = function ()...
var ProtectedMember = ...
public();
};

private(); just set up current protection level
protected(); then first checks protection level and substitutes all functions above by adapter that checks caller and calls original function
In this technique order of clauses must be imperative

"7 You shall have no other gods before me. 8 You shall not make an images in order to bow down to them or serve them. 11 You shall not take the name of the LORD your God in vain. 12 Observe the sabbath day 16 Honor your father and your mother, that your days may be prolonged. 17 You shall not kill. 18 Neither shall you commit adultery. 19 Neither shall you steal. 20 Neither shall you bear false witness against your neighbor. 21 Neither shall you covet anything that's your neighbor's." Your God

GeneralRe: How does _getAttributes work? Pin
Roger Zavala12-Oct-06 14:32
Roger Zavala12-Oct-06 14:32 
GeneralRe: How does _getAttributes work? Pin
araud13-Oct-06 3:59
araud13-Oct-06 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.