2

I wanted to show problem with this video. Please watch...

I've 2 pages. Second page is sending selected pictures to the opener window(first one) using fResimleriEkle function.

I'm setting every element of array to another array variables (ArrResimler and ArrMetinler).

But when i fire the fAlbumOlustur function by clicking to button, i can't see the values of global variables.

Is there any problem about global variables?

The problem is in the picture: alt text

Thank you for your help....

Community
  • 1
  • 1
uzay95
  • 16,052
  • 31
  • 116
  • 182
  • It's hard to understand what the problem is here (and I saw this the last time you asked too). How are the two pages related? – Pointy Mar 10 '10 at 21:15
  • Don't think two pages. One function is setting two global variable(ArrayResimler = ArrayPictures, ArrayMetinler=ArrayText). But i can't acces these variable's values in the second function. The problem is this. – uzay95 Mar 10 '10 at 21:19
  • Do think two pages, because global variables are global in the window scope.. so each page has different globals.. – Gabriele Petrioli Mar 10 '10 at 21:22
  • But these vars are in the same page. Only first function parameter is coming from child window. But i am parsing the data of array to global vars in the main page which has contain the functions. – uzay95 Mar 10 '10 at 21:27
  • I make the assumption that you actually call the first function, then the second one in sequence...hard to tell from the example picture.... – Mark Schultheiss Mar 10 '10 at 21:50
  • +1 for style. an audio explanation along with the video would have been more helpful as the video alone makes very little sense. – Anurag Mar 11 '10 at 02:38
  • See http://stackoverflow.com/questions/99927/oo-javascript-definitive-explanation-of-variable-scope. – Pierre-Antoine LaFayette Mar 10 '10 at 21:19

3 Answers3

1

Global variables are bad! Another one about how they are bad...

You could always pass the array in a "buffer" parameter in your functions which is cleaner IMO.

Example:

<script type="text/javascript">
    function WorkWithArray(myArray, someOtherParam)
    {
        if (myArray.constructor.toString().indexOf("Array") == -1)
            return false;

        //Work with myArray here
        myArray[myArray.length] = 'blah';
        return true;
    }
</script>

In JavaScript, we have functions and we have arguments that we pass into those functions. But how JavaScript handles what you’re passing in is not always clear. When you start getting into object-oriented development, you may find yourself perplexed over why you have access to values sometimes but not other times.

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function.

Passing in an object (an array is an object), however, passes it in by reference. In this case, any property of that object is accessible within the function.

See JavaScript: Passing by Value or by Reference for more info.

Community
  • 1
  • 1
AlexV
  • 22,658
  • 18
  • 85
  • 122
  • Would you explain with a shor example? – uzay95 Mar 10 '10 at 21:17
  • He wasn't asking if they were bad - wouldn't your post be a more appropriate comment seeing as you aren't actually attempting to answer the question? – Erik Mar 10 '10 at 21:21
  • Read his question. Quoting: "Is there any problem about global variables?". Anyway, added more info about my proposed solution and an example too! – AlexV Mar 10 '10 at 21:55
0

Maybe I'm missing something, but can't you just set a "watch" to the global variables you're interested in?

Robusto
  • 31,447
  • 8
  • 56
  • 77
0

Wrong line is:

 arrResims = ArrResimler = sResimler

I can't set the values again after above line.

uzay95
  • 16,052
  • 31
  • 116
  • 182