0

When an object is pushed into an array, does it refer to the same instance of the object?

Eg:

function object() {
    var count = 1;
}

array = [];

var obj = new object();
array.push(obj);

here, is the object inside the array, "array[0]", the same instance of the object outside the array, "obj"?

Also, if I was to pass obj into another function, will I be passing the same instance of the object into the function's parameters or will the function create a new object?

user3794529
  • 77
  • 2
  • 6

4 Answers4

3

When you push the object into the array, there is still only one instance of the object. The variable contans a reference to the object, and the array contains another reference to the same object.

If you change the object then the change is visible both when you view it through the variable and the array, as it's the same object. However if you change the variable (for example assigning another object to it), that won't affect the array; it will still reference the original object.

When you pass the object as a parameter to a function, the reference is passed by value. There is still only one instance of the object, but the parameter is separate from the variable that you use in the call. If you change the object inside the function the change is visible outside the function as it's the same object, but if you change the parameter that won't affect the variable that you used in the call.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Objects in javascript are passed into arrays by reference. To crib a bit of your code,

function object(){
    var count = 1;
}
array = [];
var obj = new object();
array.push(obj);
array.push(obj);
array[1]['n'] = 5

produces

array
=> [ { n: 5 }, { n: 5 } ]

This is because you're just working with a reference to the actual object. Therefore, any references to the object are the same - be they inside the array, duplicates, or what have you. If you want a deep copy, you'll need to implement that yourself.

alephtwo
  • 322
  • 2
  • 10
0

Javascript is pass by value for primitives (for objects too - but in that case, the value is a reference to the object). However, when an object is passed into an array, this value is a reference to an object. When you pass an object or array, you are passing a reference to that object, and it is possible to modify the contents of that object, but if you attempt to overwrite the reference it will not affect the copy of the reference held by the caller i.e. the reference itself is passed by value.

When you pass in a primitive (e.g. string/number), the value is passed in by value. Any changes to that variable while in the function are separate from whatever happens outside the function.

Pass by value for primitives

function testFunction(x) {
      // x is 4
      x = 5;
      // x is now 5
}

var x = 4;
alert(x); // x is equal to 4
testFunction(x); 
alert(x); // x is still equal to 4

Passing an object is by reference (pass by value but this value is a reference):

function myObject() {
    this.value = 5;
}
var o = new myObject();
alert(o.value); // o.value = 5
function objectchanger(fnc) {
    fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6

Passing in a method of an object is not passed by reference though (due to the lost context when you pass in a function as a parameter).

ali haider
  • 19,175
  • 17
  • 80
  • 149
-1

Everything not a primitive type is passed by reference.

LJᛃ
  • 7,655
  • 2
  • 24
  • 35
  • Everything in JavaScript is pass by value. Always. – newacct Jul 31 '14 at 02:54
  • Would you mind elaborating on this? As stated here: http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference everything not primitive is passed by "copy of reference" and thus still a reference. – LJᛃ Jul 31 '14 at 22:48
  • So while the wording may be misleading for people coming from languages that support proper pass by value/reference its essentially correct to say that everything non primitive is passed as _a_ reference which sufficiently answers the question. – LJᛃ Jul 31 '14 at 22:59
  • 2
    Everything non-primitive is not "passed as" a reference -- everything non-primitive *is* a reference, just like in Java. There is nothing special about "passing" compared to any other operations. Every value in JavaScript is either a primitive or a reference. When you create an object, you get a reference back; when you access a member, the left side of the dot takes a reference; basically, any time you have a non-primitive in JavaScript, you have a reference. – newacct Aug 01 '14 at 01:10