CrxOop CrxOop: Bringing Object Oriented Programming, and Proper Prototype Based Programming, To Javascript
The aim of the library documented below is to provide developers with a solution to allow development using interfaces and classes as they are known in other object oriented programming (OOP) languages such as C++, C#, Java and PHP. Further more, V1.4 introduces structures, a generalization of the concept of prototypal inheritance, known here as POBP.
Subject
Body*
Email
SUBMIT CLOSE
Menu

5.5.1 this, THIS

'this' and 'THIS' are perhaps the most important of the structure keywords to understand. Internally, a structure instance consists of a shared public memory sector, a shared private memory sector, and a private memory sector. The 'this' keyword, which is the native Javascript 'this' keyword has access to all three sectors. 'THIS' on the other hand has access only to the shared public memory sector. This is why passing 'this' to functions as parameters, or from functions as returns, is dangerous and must never be done. Instead you would pass around 'THIS'.

JS (Tree)
crx_registerStructure("ExampleStructure",
{
   SHARED:
   {
      PUBLIC:
      {
         VARS:
         {
            "sharedPublicVar": 1
         },
         FUNCTIONS:
         {
            "sharedPublicFunction": function(pExampleStructure)
            {
               //   Remember 'THIS' is a structure keyword and hence is found on
               //      'this' and can be accessed using either 'this':
               console.log(this.THIS.sharedPublicVar);
               //      although for clarity you should never use 'THIS' to
               //      access instance members but instead use
               console.log(this.sharedPublicVar);

               //   Or the object that is the structure instance:
               console.log(pExampleStructure.THIS.sharedPublicVar);
               //      but again you should never use 'THIS' to access
               //      instance members but instead use
               console.log(pExampleStructure.sharedPublicVar);

               //   'THIS' is only meant for passing around. When wanting to
               //      return the instance from functions, instead of the
               //      dangerous code "return this", you would use
               return this.THIS;
            },
            "sharedPublicFunction2": function()
            {
               //    And when wanting to pass the instance to functions,
               //      instead of passing 'this', you would pass
               //       "this.THIS" like the following
               var vReturn = this.sharedPublicFunction(this.THIS);


               //   Imagine the danger of the following if sharedPublicFunction
               //      above returned 'this' instead of 'THIS'
               return vReturn;
            }
         }
      }
   }
});
JS (Verbose)
crx_registerStructure("ExampleStructure",
{
   "VERBOSE": 1,
   "shared public var sharedPublicVar": 1,
   "shared public function sharedPublicFunction": function(pExampleStructure)
   {
      //   Remember 'THIS' is a structure keyword and hence is found on
      //      'this' and can be accessed using either 'this':
      console.log(this.THIS.sharedPublicVar);
      //      although for clarity you should never use 'THIS' to
      //      access instance members but instead use
      console.log(this.sharedPublicVar);

      //   Or the object that is the structure instance:
      console.log(pExampleStructure.THIS.sharedPublicVar);
      //      but again you should never use 'THIS' to access
      //      instance members but instead use
      console.log(pExampleStructure.sharedPublicVar);

      //   'THIS' is only meant for passing around. When wanting to
      //      return the instance from functions, instead of the
      //      dangerous code "return this", you would use
      return this.THIS;
   },
   "shared public function sharedPublicFunction2": function()
   {
      //    And when wanting to pass the instance to functions,
      //      instead of passing 'this', you would pass
      //       "this.THIS" like the following
      var vReturn = this.sharedPublicFunction(this.THIS);


      //   Imagine the danger of the following if sharedPublicFunction
      //      above returned 'this' instead of 'THIS'
      return vReturn;
   }
});