This is a question on how javascript (or at least sphere/minipshere's implementation of it) handles scope when it comes to arrays and objects; this may just be a problem arising from my scripting/programming knowledge being 90% self taught - I don't know how some of these things work.
Suppose I create an array inside a function:
function my_func()
{
var my_array = [];
...
}
Now further suppose I wish to update that array inside another function:
function other_func()
{
...
my_array[2] = 5;
...
}
Obviously this will not work as the array is not in scope, now let's suppose instead I pass the array as a parameter to other_func:
function my_func()
{
var my_array = [];
other_func(my_array);
...//do something else with my_array which relies on the 3rd value being 5
}
function other_func(param)
{
param[2] = 5;
}
The second function will now not have a scope problem, but I think I'm right in saying that back in the first function will also have that value of 5 available to it, is this right? And is it appropriate to rely on this behaviour? I had instead been intending to have other_func return the array after updating it and have my_func overwrite the array with the returned one - is this functionally different?
function my_func()
{
var my_array = [];
my_array = other_func(my_array);
...//do something else with my_array which relies on the 3rd value being 5
}
function other_func(param)
{
param[2] = 5;
return param;
}
Is either of the above suggestions better than the other?
Is there any difference if the array is a property of a different object and you pass either the full object my_obj or just the array my_obj.my_array as a parameter?
Can getting this wrong create memory leaks?