1
 var a = [false, 3];
 console.info('a: '+a);
     (function(item){
            var jamie = item;
        jamie [1]--;
         console.info('jamie:  '+jamie);
     })(a);
 console.info('a: '+a);

In my mind a[1] should always equal 3 in this javascript.

And when not using an array it works as expected:

 var a = 3;
 console.info('b: '+a);
     (function(item){
            var jamie = item;
        jamie--;
         console.info('jamie b:  '+jamie);
     })(a);

console.info('b: '+a);

Why does a[1] output 2 after I have ran this JS? Fiddle of said problem :O

0

2 Answers 2

3

Because you're providing a reference to the object not passing it by value;

If you want to clone the object, here is a helpful function to allow it:

function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

var a = [false, 3];
console.info(a); // [false, 3];
    (function(item){
        var jamie = clone(item);
        jamie [1]--;
        console.log(jamie); // [false, 2];
    })(a);
console.info(a); // [false, 3];
Sign up to request clarification or add additional context in comments.

7 Comments

He is passing "a" by value; its value is a reference to the array object.
Right, what Pointy said :)
Sorry to be so pedantic but blurring the meaning of the word "reference" with the term "pass by reference" has been driving me slightly crazy for decades :)
Boy I need to get better at JS.
heh, your "backwards" if statement variables threw me off. You sly cat
|
1

This has nothing to do with closures.

You're passing a reference to an array into a function, and the function is using that reference to alter the value of a property of the array.

When you assign an object (arrays are objects) to a variable, you're setting the value of the variable to a reference to the object. You can't manipulate objects as values directly; all you have are references.

4 Comments

For clarity could you please explain how one might pass it by value and not by reference?
@JamieHutber you are passing by value. The value of variable "a" is a reference to the array. It is not possible to pass the array itself as a value without your explicitly making a copy of the array. (In that case, you'd be passing a reference to the copy of the array.)
I see, so because its an object (all objects are passed by reference) I can not pass it as a new value. Interesting, interesting that I knew this in theory. But this is the first time it has confused me. So to not alter the array's details I should put a[1] in as the value of the annoyomus function.
@JamieHutber yes, or pass a.slice(0) - that'd pass a copy (a shallow copy) of the array.

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.