8 Replies Latest reply on Aug 18, 2014 5:04 AM by Marc Autret

    function call another function variable

    creativejoan0425 Level 1

      Is it possible to call another function variable?

      Below is my Script:

       

      f2();

       

      function f1() { 

        var v2 = 1; 

      function f2(v2) { 

        var v3= 2; 

        alert(v2);

      }

        • 1. Re: function call another function variable
          pixxxel schubser Level 5

          Do you mean something like this?

          var v2 = v3 = null;
          f1 (v2);
          f2 (v2);
          alert("v3 = " + v3 + " after function f2");
          
          function f1(x) {
              v2 = 1;
              alert("v2 = " + v2 + " in function f1");
              return v2;
              }
          
          function f2(x) {
              v2 ++;
              v3 = v2;
              alert("v2 = " + v2 + " in function f2");
              return v3;
              } 
          
          • 2. Re: function call another function variable
            creativejoan0425 Level 1

            The variable not number, it is string, so that not what I want

             

             

            f2();

            function f1() {

              var v2 = "test";

            }

            function f2(v2) {

              var v3= 2;

              alert(v2);

            • 3. Re: Re: function call another function variable
              pixxxel schubser Level 5

              creativejoan0425,

               

              Hmmh?

              Das Prinzip ist doch das gleiche:

              var v2 = v3 = null;
              var someText = "Text";
              f1 (someText);
              f2 ();
              alert("v3 = " + v3 + " (after function f2)");
              
              function f1(any) {
                  v2 = any;
                  alert("v2 = " + v2 + " (in function f1)");
                  return v2;
                  }
              
              function f2() {
                  v2 += "beispiel";
                  v3 = v2;
                  alert("v2 = " + v2 + " (in function f2)");
                  return v3;
                  }
              
              
              

               

              Have fun

               

              • 4. Re: function call another function variable
                pixxxel schubser Level 5

                Hey creativejoan0425,

                do you understand my last answer – or not?

                 

                • 5. Re: function call another function variable
                  creativejoan0425 Level 1

                  little bit, coz I'm the new learner in javascript, I try to put the concept in my script my not work:

                   

                  below is not the full version of my script, coz it to long, so I make it simple, I juz want to alert(myA) and the result should be "Test" without changing myFun3(eachFrames) name, coz it related to myFun1

                  myFun2();

                  myFun1("LowRes", "PDF_fit on paper", "Smallest File Size PDF");

                   

                   

                  function myFun1(psFolder, preset, description, filePrefix) {

                    for (var i = app.documents.length -1;  i >= 0; i--) {

                    var aDoc = app.documents[i];

                   

                   

                    for (k=0; k<aDoc.pages.length; k++) {

                          if (myFun3(aDoc.pages[k].textFrames)){

                    PageList += aDoc.pages.item(k).name + ", ";

                    }

                    }

                   

                   

                      }

                  }

                  function myFun2(){

                  var myA = "Test";

                  }

                  function myFun3(eachFrames){

                      alert(myA);

                     

                      }

                  • 6. Re: Re: function call another function variable
                    pixxxel schubser Level 5

                    Hi creativejoan0425,

                    still much to learn.

                    … I try to put the concept in my script

                    I don't see your try.

                    You see my example, but you don't use this information – why not?

                     

                    All the same.

                    Does this work for you?

                    var myA = null; // declare at first your variable myA
                    myFun2();
                    
                    myFun1("LowRes", "PDF_fit on paper", "Smallest File Size PDF");
                    
                    function myFun1(psFolder, preset, description, filePrefix) {
                        for (var i = app.documents.length -1;  i >= 0; i--) {
                            var aDoc = app.documents[i];
                          
                            for (k=0; k<aDoc.pages.length; k++) {
                                if (myFun3(aDoc.pages[k].textFrames)){
                                    PageList += aDoc.pages.item(k).name + ", ";
                                    $.writeln(PageList)
                                    }
                                }
                          
                            }
                        }
                    
                    function myFun2() {
                        // give now myA the new value
                        myA = "Test";
                        // it is necessary  to "return" the new value of myA
                        return myA;
                        }
                    
                    function myFun3(eachFrames) {
                        alert(myA);
                        return; // every function should be "returned" !
                        }
                    
                    
                    
                    • 7. Re: Re: function call another function variable
                      creativejoan0425 Level 1

                      thx very much

                      you solve my problems

                      I want to ask why every function need to return?

                      • 8. Re: Re: Re: function call another function variable
                        Marc Autret Level 4

                        Hi creativejoan0425,

                         

                        Some additional notes:

                         

                        > Is it possible to call access another function variable?

                         

                        No, it isn't. A local variable only lives in the scope of the function (body) where it is declared. In that sense any local variable should be considered private—in OOP terms.

                        pixxxel schubser's solution is based on setting a global scope for the variable myA, so it is visible from every place of your code.

                         

                        Another solution could be implemented using JS closure mechanism. You can create a local scope (in an anonymous function which is executed once) then return your main function and additional helpers from that scope. All inner functions have then access to the local variables that have been declared in the scope. For example, you can create your function f1 and attach a getter function to it, as follows:

                         

                        var f1 = (function(/*anonymous*/__)
                        {
                            // Local variable to be nested in the closure
                            // ---
                            var v1;
                            
                            (__ = function(/*optional init value*/x)
                            {
                                // Here is your *actual* function
                                // do what you want with v1
                                // ---
                                v1 = 'undefined'!=typeof x ? x : Math.random();
                            }
                            ).getVar = function()
                            {
                                // Here is your getter
                                // ---
                                return v1;
                            };
                            
                            return __;
                        })();
                        
                        
                        var f2 = function()
                        {
                            alert( f1.getVar() );
                        };
                        
                        
                        // Process
                        // ---
                        f1('init');
                        f2();           // => 'init'
                        
                        f1();
                        f2();           // => some random number
                        
                        f1();
                        f2();           // => another random number
                        // etc
                        
                        

                         

                        This way v1 remains almost private but the outer code can read it through f1.getVar().

                         

                        Anyway this seems to me a complicate approach for a basic script that probably does not require high security level. Another option, really easy to set up, is to use the fact that a function is an object. Instead of declaring a local variable, just handle v1 as a (public) member of f1, as follows:

                         

                        var f1 = function F(/*optional init*/x)
                        {
                            // Do something with F.var
                            // ---
                            F.v1 = 'undefined'!=typeof x ? x : Math.random();
                        };
                        
                        var f2 = function()
                        {
                            alert( f1.v1 );
                        }
                        
                        f1('init');
                        f2();          // => 'init'
                        
                        f1();
                        f2();          // => some random number
                        
                        f1();
                        f2();          // => another random number
                        
                        
                        
                        
                        

                         

                        That's it!

                         

                        > why every function need to return?

                         

                        No, return is not required. By default, any function that has no return statement will simply return undefined.

                         

                        @+

                        Marc