1

I'm trying to pass a variable by reference, but it isn't working like it should (or I've hoped it would).

boardCopy is still empty after callMeMaybe has been called and I don't know why. If I copy the board already in the first function with boardCopy = board.slice(0) and don't do this in the second function it's working, but that isn't an option, because the real board will be much bigger and callMeAnytime will be a recursive function and much more complex.

callMeAnytime(3, [], [1, 0, 1, 1]);

function callMeAnytime(index, moves, board) {
    for (var i = index; i >= 0; i--) {
        var boardCopy = [];
        callMeMaybe(i, board, boardCopy)
        console.log(board); // [1, 1, 1, 1]
        console.log(boardCopy); // []
    }
}

function callMeMaybe(i, board, boardCopy) {
    if (board[i] == 1) {
        boardCopy = board.slice(0);
        boardCopy[i] = 0;
        console.log(board); // [1, 1, 1, 1]
        console.log(boardCopy); // [1, 1, 1, 0]
    }
}
3
  • So what does the Javascript debugger in your browser's developer tools say about all this? Commented Jun 23, 2013 at 0:24
  • Anyway, boardCopy = ... replaces the value of the local variable boardCopy with the right hand side of the assignment. Why would you expect it to change anything in the calling function? Javascript doesn't even have pass-by-reference semantics. Commented Jun 23, 2013 at 0:26
  • 2
    Pass-by-reference is a well defined term for parameter passing. JavaScript does not support pass by reference, only pass by value. In case of arrays and objects the value is a reference to the object. You can modify the object itself, but you cannot assign a different value to variable (and expect it to be changed outside after the function call). Commented Jun 23, 2013 at 0:28

2 Answers 2

2

As has been mentioned, javascript uses pass-by-value, you can modify the objects elements/properties but not replace the object itself. slice returns a new array to the object. You will have to something like this instead.

slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array.

function callMeMaybe(i, board, boardCopy) {
    if (board[i] == 1) {
        //boardCopy = board.slice(0);
        var length = board.length,
            j = 0;

        boardCopy.length = 0;
        while (j < length) {
            if (Object.prototype.hasOwnProperty.call(board, j)) {
                boardCopy[j] = board[j];
            }

            j += 1;
        }

        boardCopy[i] = 0;
        console.log(board);
        console.log(boardCopy);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, got it now, will do it like this.
you can also use [].push.apply(boardCopy, board); instead of the loop and conditional, tends to be faster...
true, though depending on the size of the array you could exceed the maximum stack with that method.
1

You're assigning a new value to the local variable. It works when you do the splicing in callMeAnytime because that gets assigned to the "master/original variable" (which is passed to callMeMaybe, so callMeMaybe doesn't reassign the local variable)

Edit: And as millimoose stated, you can't pass by reference in Javascript in the first place. If you give us more details on what you're trying to do, we can probably point you in the right direction.

1 Comment

The OP is passing the array by value, which is also the source of his problem. To the best of my knowledge you can't pass variables by reference in Javascript at all.

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.