adding a object or array through a function wont work!

● ARCHIVED · READ-ONLY
Started by Monkey BizNiz 6 posts View original ↗
  1. Hey guys,

    Why cant I add a new array or object through this way:
    Code:
    function blabla(location) {
    location = [];
    };
    var examplevar = [];
    blabla(examplevar[4])

    When it does work if i do this, which should be the same thing?:

    Code:
    [CODE]function blabla(location) {
    examplevar[4] = [];
    };
    var examplevar = [];
    blabla(examplevar[4])
    [/CODE]

    Thanks in advance,
  2. I believe it might because the function parameters take the value of the parameter, not its address or pointer so when you called blabla(array[4]) it actually took whatever the current value stored in array[4] rather than sending array[4] itself and because of that, your parameter = []; didn't work

    Think of the array maybe as a candybox, now you want to send it to someone to fill one of the spaces in that box. But when you sent the empty slot to that someone, instead of him getting the empty slot in the candy box so that he can fill it, what he received was just the fact that the slot was "Empty"
  3. ahh, i was afraid it mightve been something like that.

    Do you perhaps know of any way to use functions the way i intend them to work, like in the first example tho? I feel like it should be possible somehow to use fuctions to change values like that.
  4. Sadly my JS knowledge doesn't cover how to do that.
  5. If I understand well, you could do this:
    Code:
    function blabla() {
         return   [];
    };
    var examplevar = [];
    examplevar[4] = blabla();
    -------------------------------------------------------------- (Next: Another possible option )
    Code:
    var myFunction = function(element){
            element[4] = [];
            return element;
    };
    
    var examplevar = [];
    examplevar = myFunction(examplevar);
    Really, for be an array, your code is correct and it should work (your [4] should be another array)... But if not, this is another safe option. :kaoslp:

    I think. If else, I'm sorry :kaoswt2:

    Cheer up!
  6. Zausen said:
    If I understand well, you could do this:
    Code:
    function blabla() {
         return   [];
    };
    var examplevar = [];
    examplevar[4] = blabla();
    -------------------------------------------------------------- (Next: Another possible option )
    Code:
    var myFunction = function(element){
            element[4] = [];
            return element;
    };
    
    var examplevar = [];
    examplevar = myFunction(examplevar);
    Really, for be an array, your code is correct and it should work (your [4] should be another array)... But if not, this is another safe option. :kaoslp:

    I think. If else, I'm sorry :kaoswt2:

    Cheer up!
    Worked great, thanks mate!