Announcement

Collapse
No announcement yet.

efsInternal() and global vars question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • efsInternal() and global vars question

    Hello,

    I am looking to call efsInternal() multiple times, with different input vars, but each require having their own set of "global vars". My idea was to create an array that gets passed in, it does some work on the values of that array, then passes the array back with the updated vars. These arrays would be stored at the main's global scope.

    The fact that I am calling efsInternal() multiple times, using different input vars means I can't use normal global vars (I'm assuming), so I have to come up with some way to have each one keep track of its own state.

    I have two questions. One, is this the way I should approach this to begin with? Two, what is the best way to do this performance-wise (is there a way to call efsInternal() once in an Init instead of each bar? Is calling getSeries() each time very slow? etc.) ?


    (I'm not sure if the following is syntactically correct)

    PHP Code:

    // Initial setting of the arrays (to hold the "global" vars for each call to efsInternal()
    var xGlobalVars1 = new Array(0,1);
    var 
    xGlobalVars2 = new Array(2,3);

    function 
    main() {

        if (
    getBarState() == BARSTATE_NEWBAR)   {

            
    // Call the function, passing in the "global var array", then getting back the updated arrays to be stored at this scope
            
    xGlobalVars1 efsInternal("doWork"xGlobalVars1 );
            
    xGlobalVars2 efsInternal("doWork"xGlobalVars2 );
        }
    }



    function 
    doWork(myGlobalVars) {

        
    // Retrieve the individual global vars and put them into local vars
        
    var myVar1 getSeries(myGlobalVars0);
        var 
    myVar2 getSeries(myGlobalVars1);

        
    // Do some kind of work on the vars
        
    myVar1++;
        
    myVar2++;

        
    // return the "global vars" in an array (which is later passed back into this function to do work again)
        
    return new Array(myVar1myVar2);

    Any help is greatly appreciated! Thanks!
    Daniel
    eSignal File Share: http://share.esignal.com/groupcontents.jsp?groupid=1130

  • #2
    I will reply to my own question.

    It seems that when calling efsInternal() creates its own set of global variables, so my entire approach was wrong.

    I believe this is more of what I am wanting.

    (As an aside, how do I not have all the extra line breaks in the PHP block???)

    PHP Code:

    var xResult1 null;
    var 
    xResult2 null;

    var 
    bInit false;

    function 
    main() {

        if (!
    bInit) {
            
    xResult1 efsInternal("doWork"1);    // can also add "inv("D")" to the end of the param to have its own interval
            
    xResult2 efsInternal("doWork"5);
            
    bInit true;
        }

        if (
    getBarState() == BARSTATE_NEWBAR)   {

            var 
    Value1A getSeries(xResult10).getValue(0);
            var 
    Value1B getSeries(xResult11).getValue(0);

            var 
    Value2A getSeries(xResult20).getValue(0);
            var 
    Value2B getSeries(xResult21).getValue(0);
        }
    }


    var 
    myVar1 0;
    var 
    myVar2 0;
    function 
    doWork(Input) {

        
    // Do some kind of work on the vars
        
    myVar1 += Input;
        
    myVar2 += Input;

        return new Array(
    myVar1myVar2);

    Last edited by neoikon; 12-20-2011, 10:59 AM.
    eSignal File Share: http://share.esignal.com/groupcontents.jsp?groupid=1130

    Comment


    • #3
      This is unrelated to your post but it is a comment on your variable declaration style.

      Javascript does not have block scope.

      So, always do the following:

      1. Declare all global variables at the top of your file before any functions are declared.

      2. Declare all local variables of a function after the first "{".

      There's a lot of bad parts to Javascript and this is one of them.

      Comment


      • #4
        Originally posted by SteveH
        This is unrelated to your post but it is a comment on your variable declaration style.

        Javascript does not have block scope.

        So, always do the following:

        1. Declare all global variables at the top of your file before any functions are declared.

        2. Declare all local variables of a function after the first "{".

        There's a lot of bad parts to Javascript and this is one of them.

        Thanks for the reply. The example code was very rough in order to give a simplistic example of what I was actually trying to achieve, so don't look too close at it. ;]

        Though, you bring up an interesting point. So, you're saying that any variables declared in an "if" block won't have scope specific to that block. But, what is the benefit or downside of declaring it later (versus right after the first "{"? Similarly, what is the benefit/downside to defining global vars NOT at the top? For example, after preMain() but before main()?

        Daniel
        eSignal File Share: http://share.esignal.com/groupcontents.jsp?groupid=1130

        Comment


        • #5
          Only the declarations of your variables are hoisted to the top of their scope, not their initializations. If you reference a variable prior to its initialization location, it's value will be 'undefined'.

          Comment

          Working...
          X