Quick question on Javascript to which I can't find a clear concise answer.
I'm building an app that's way ahead of anything I've done before and involves multiple classes being instantiated. These objects are then passed into a processing class that checks user inputs, draws onto canvas and updates the objects that it has been passed.
I am wondering, how does JavaScript handle passing objects to functions? Am I passing a copy of the object, or am I passing a reference to the object?
So if my controller class alters one of the objects variables, is that changed everywhere or just in the object that that controller sees?
Sorry for such a simple, possibly easily testable question but I'm not even sure if I'm making a class correctly at this point thanks to errors piling up.
Quick question on Javascript to which I can't find a clear concise answer.
I'm building an app that's way ahead of anything I've done before and involves multiple classes being instantiated. These objects are then passed into a processing class that checks user inputs, draws onto canvas and updates the objects that it has been passed.
I am wondering, how does JavaScript handle passing objects to functions? Am I passing a copy of the object, or am I passing a reference to the object?
So if my controller class alters one of the objects variables, is that changed everywhere or just in the object that that controller sees?
Sorry for such a simple, possibly easily testable question but I'm not even sure if I'm making a class correctly at this point thanks to errors piling up.
Share Improve this question edited Jan 31, 2017 at 19:06 SCGH 9678 silver badges14 bronze badges asked Jun 28, 2013 at 8:53 Chris MorrisChris Morris 9832 gold badges18 silver badges41 bronze badges 11- google./… – C3roe Commented Jun 28, 2013 at 8:55
-
You're passing a reference. Assignments
obj1 = obj2
also just create more references to the same object. – nnnnnn Commented Jun 28, 2013 at 8:56 - in javascript objects are passed by reference – user405398 Commented Jun 28, 2013 at 8:57
- 1 Did you see this answer? I see your pain as the answers are ambiguous but this cleared things up for me. Best bet is to do a bit of experimenting with jsFiddle for clarity :-) – Stokedout Commented Jun 28, 2013 at 9:02
- Thanks for the link, I'm not too sure of the syntax but it does illustrate the way i;ve been doing it in the second part and nice to know it is being done correctly. 200 lines of code and 4 hours last night weren't wasted. – Chris Morris Commented Jun 28, 2013 at 9:08
3 Answers
Reset to default 4When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are pletely separate from anything that happens outside the function.
function myfunction(x)
{
// x is equal to 4
x = 5;
// x is now equal to 5
}
var x = 4;
alert(x); // x is equal to 4
myfunction(x);
alert(x); // x is still equal to 4
Passing in an object, however, passes it in by reference. In this case, any property of that object is accessible within the function
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
As well described in https://stackoverflow./a/5314911/636348, in JavaScript it's always pass by value, but for objects the value of the variable is a reference.
So, it's not a pure pass by reference. Here's an example to understand this concept:
E.g.:
x = {member:"foo"}
If you change the object with another object inside a function, you won't get the new object outside the function scope because you just create another object. The original reference is still bound to the original object:
function changeObject(x) {
x = {member:"bar"};
}
changeObject(x);
alert(x.member)
output: foo
instead, if you alter a member inside a function, the object will be changed:
function changeMember(x) {
x.member = "bar";
}
changeMember(x);
alert(x.member)
output: bar
If you pass in a variable which is pointing to an object, it passes a reference to the object. If you pass in an object literal, then obviously no other class or function will be able to change that object.