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?

3
  • The count is just a property of the object. Commented Jul 30, 2014 at 19:19
  • 1
    I take it you're asking if the object is passed by value or reference? Commented Jul 30, 2014 at 19:20
  • 2
    Have you tried doing obj.count = 2; console.log(array[0].count); and seeing what it outputs? Commented Jul 30, 2014 at 19:21

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

What about functions? Does it do it by reference?
you lost the context when you pass in a function as a parameter
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).

Comments

-1

Everything not a primitive type is passed by reference.

4 Comments

Everything in JavaScript is pass by value. Always.
Would you mind elaborating on this? As stated here: stackoverflow.com/questions/13104494/… everything not primitive is passed by "copy of reference" and thus still a reference.
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.
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.

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.