Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to pass a var trough functions?

Participant ,
Jun 12, 2013 Jun 12, 2013

Can someone explain how to pass a var trough functions?

Daniel

TOPICS
Scripting
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 12, 2013 Jun 12, 2013

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);

    };

Translate
Adobe
Guru ,
Jun 12, 2013 Jun 12, 2013

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…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 12, 2013 Jun 12, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2013 Jun 12, 2013

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);

    };

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 12, 2013 Jun 12, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2013 Jun 12, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2013 Jun 12, 2013

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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 13, 2013 Jun 13, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jun 12, 2013 Jun 12, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines