4 Replies Latest reply on Sep 16, 2007 10:19 AM by Ardy15jan

    Create a custom "top-level" function

    Ardy15jan Level 1
      Hi, everybody !
      I have some custom made functions that I tend to use quite often. I`ve put them in a class and defined them as 'static', but I`m not such a big fan of this procedure. I want to make a class that contains their definitions, import it and then just use the functions as if they were defined in my current class. Does anybody know how this could be achieved?
      Thanks!
        • 1. Re: Create a custom "top-level" function
          Juankpro Level 2
          One method is always inheriting from this class. This ways your new class has access to this methods.

          Another method is creating what is usually called a mix-in. You class must have an static initialize(obj) method which receives any object or prototype and adds methods to it. You will have to at least declare this mixed-in properties though (onle declare them as properties, do not implement them).

          Another solution, if you want the functions available everywhere is to create _global methods.

          The last solution I can think of is a class using the Singleton pattern, allow the creation of only one global instance allowing access to this methods. To create this singleton the normal procedure is to make constructor private, then create an static get method which asks if an instance as already been created and if not (first time) creates one. Like the Camera or Microphone classes do.
          • 2. Re: Create a custom "top-level" function
            Ardy15jan Level 1
            Thanks for all the options you gave me!
            The one I`m really interested in is the mix-in one. Could you please give me some details and an example? Thanks again!
            • 3. Re: Create a custom "top-level" function
              Juankpro Level 2
              The Class:

              class MyClass{
              public function method1(){
              // Your method here
              }

              public function method2(){
              // Your method here
              }

              private static instance:MyClass;

              public static function initialize(obj):Boolean{
              if(instance == undefined){
              instance = new MyClass();
              }
              obj.method1 = instance.method1;
              obj.method2 = instance.method2;
              return true;
              }
              }



              How to use it:
              usage 1:

              class ProjectClass{
              public var method1:Function;
              public var method2:Function;

              public function ProjectClass(){
              MyClass.initialize(this);
              }
              }

              How to use it:
              usage 2:

              class ProjectClass{
              private static var initialized:Boolean = MyClass.initializeClass(ProjectClass.prototype);

              public var method1:Function;
              public var method2:Function;
              }
              • 4. Re: Create a custom "top-level" function
                Ardy15jan Level 1
                Thaks, Juank! I`ll have a go with this mix-in class, and I`ll also try declaring the methods globaly, to see which one works best for me.
                Thanks again! I really apreciate your help!