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?
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.
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.
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).
Everything not a primitive type is passed by reference.