0
this.runThisFunctionOnCall = function(){
        

    //does something and ends up with these arrays of count 1

    // arrays.length = 1 (always)

    array1;
    array2;
    array3;

    alert(array1.length); // shows 1 here 
                    
    }

On a different script page, that includes the above page when I do the below, I have no values in array:

this.callingFunction = function(){
            
        this.runThisFunctionOnCall();
        alert(array1.length); shows 0

}
3
  • 4
    How and where are the variables defined? This is probably a case where you should include more of your actual code. Commented Apr 22, 2011 at 20:59
  • 1
    runThisFunctionOnCall doesn't return anything. Does it change a global variable? Commented Apr 22, 2011 at 21:01
  • @cap - I presume he wants to know why the reference to array1 works in one place but not the other, but we need more actual code to answer that one accurately. Commented Apr 22, 2011 at 21:02

1 Answer 1

3

The variable array1 is a local variable inside the function runThisFunctionCall so you can not see it outside the function and that leads to array1 in your lower code being another array1 and not the same as in the function and having the length 0 because there are no elements in it.

Here is some explaining: http://www.webdevelopersnotes.com/tutorials/javascript/global_local_variables_scope_javascript.php3

Sign up to request clarification or add additional context in comments.

1 Comment

According to code this "The variable array1 is a local variable inside the function runThisFunctionCall" is simply wrong. And before "having the length 0" you will get "variable not found" exception. So it is not correct either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.