8 Replies Latest reply on Jun 13, 2013 1:25 AM by Muppet Mark

    How to pass a var trough functions?

    Damienned

      Can someone explain how to pass a var trough functions?

       

      Daniel

        • 1. Re: How to pass a var trough functions?
          Muppet Mark Level 5

          Pass as named arguments…? If you are passing many ( say from dialog input ) then I put them in an Object() and pass that as agrument…

          • 2. Re: How to pass a var trough functions?
            Damienned Level 1

            Hi Mark, thanks for your reply, here's an example of wat i mean, i want to pass the itemsearchcount var from one button to another inside a function.

            Is this possible, could you show me.

             

            function previewdata(){

             

                 datapreview.backgroundgroup.backgroundgroupmarge.searchdata.searchdatapreviousBtn.onClick = function () {

                      var itemsearchcount = 1;

                 };

             

                 datapreview.backgroundgroup.backgroundgroupmarge.searchdata.searchdatanextBtn.onClick = function () {

                      alert(itemsearchcount);

                 };

             

            };

             

            It is just part of the function just to make it clear.

             

            Thanks Daniel

            • 3. Re: How to pass a var trough functions?
              pixxxel schubser Level 5

              Do you mean something like this?

               

               

              var itemsearchcount = null;
              
              previewdata();
              nextviewdata();
              
              function previewdata(){
                  // do something
                  itemsearchcount = 1;
                  return itemsearchcount;
                  };
              function nextviewdata(){
                  // do something more
                  alert(itemsearchcount);
                  };
              
              • 4. Re: How to pass a var trough functions?
                W_J_T Level 4

                Or this?

                 

                function testFunc(varInfo) {
                    alert("varInfo " + varInfo);
                }
                //
                testFunc(5)
                testFunc("hi")
                

                 

                Edit: unless it was supposed to be through functions as in from one to another?

                • 5. Re: How to pass a var trough functions?
                  Damienned Level 1

                  This worked for me, but it should be possible to create a var within a function and then call the var within a other function.

                   

                  Thanks Daniel

                  • 6. Re: How to pass a var trough functions?
                    pixxxel schubser Level 5

                    No, that's not possible.

                    Define one or more empty (global) var before you go into a function. You can use them later (also in a function and give back to others).

                     

                    IMHO a (local) var, which was created in a function is always undefined outside of this function.

                    • 7. Re: How to pass a var trough functions?
                      CarlosCanto Adobe Community Professional & MVP

                      that example worked by using Global variables, by definition variables declared inside a function are visible only within such function...I don't completely understand what you're trying to accomplish but from your code snippet above, maybe you want something like this?

                       

                      is this the reference to your button? consider using variables for buttons if you need to refer to them later in your code

                      datapreview.backgroundgroup.backgroundgroupmarge.searchdata.searchdatapreviousBtn
                      

                       

                      var win = new Window ("dialog", "test dialog");
                      var prevBtn = win.add ("button", undefined, "prev");
                      var nextBtn = win.add ("button", undefined, "next");
                      
                      prevBtn.onClick = function () {
                          this.itemsearchcount = 1;
                      }
                      
                      nextBtn.onClick = function () {
                          alert(prevBtn.itemsearchcount);
                      }
                      
                      win.show();
                      
                      • 8. Re: How to pass a var trough functions?
                        Muppet Mark Level 5

                        I would still do like so…

                         main();
                        
                        function main() {
                        
                            var win = new Window ("dialog", "test dialog");
                            var prevBtn = win.add ("button", undefined, "prev");
                            var nextBtn = win.add ("button", undefined, "next");  
                           var count = 0;
                             
                            prevBtn.onClick = function () {
                                count++
                                alert( count);
                            }
                             
                            nextBtn.onClick = function () {
                                count--
                                alert( count);
                            }
                             
                            win.show();
                        
                        };
                        
                        alert( count ); // Undefined