wwaawaw
wwaawaw

Reputation: 7127

When a variable is passed to a function, what actually gets passed?

I was told on another thread that after the function exits, any modifications made to its operands from within the function will persist. I always thought that it made a temporary copy of all the values passed into it, and then the only things that persisted were return values and implicitly more broadly scoped variables that were modified.

I suppose thinking back to all the jquery plugins whose source code I've peaked at, they all use the construct:

(function($){
  $.fn.foo = function(){ console.log('foo'); };
})(jQuery);

which imply that modifications to the jQuery object even by the internal-scope $ identifier persist after the function exits, or the jQuery plugins would not work. So, this works as the above snippet does:

var x = {n:0};

(function addOneTo(p) {
  p.n = p.n + 1;
})(x);

console.log(x);

But this:

var x = 0;

(function addOneTo(p) {
  p = p + 1;
})(x);

console.log(x);

doesn't, leaving x unmodified with a value of 0.

Could someone just explain how argument passing works? I thought I knew how it did, but I guess I don't. Thanks

Upvotes: 1

Views: 104

Answers (3)

The Alpha
The Alpha

Reputation: 146191

Actually in your first example

var x = {n:0};
(function addOneTo(p) {
    p.n = p.n + 1;
})(x);
console.log(x);

The x is an object and when you used p.n = p.n + 1; it modified the original object's property because it passed as a reference (p is the same object as x) but in your second example

var x = 0;
(function addOneTo(p) {
    p = p + 1;
})(x);
console.log(x);

x is passed by a value in to the function and p = p + 1; here p is a new private variable inside that function and it has only the function scope.

Upvotes: 1

João Silva
João Silva

Reputation: 91299

When you have:

p.n = p.n + 1;

What you are doing is modifying a property called n in the object referred to by p. In other words, p can be seen as a pointer to an object, that is, it holds a reference to the original object whose reference value you have passed to the function. On other hand, when you have:

p = p + 1;

You are simply replacing the object reference value held by p with the result of evaluating the expression p + 1. You are not modifying any property of the p object.

Upvotes: 1

jsbueno
jsbueno

Reputation: 110261

It is actually very simple: every name in Javascript points to an object in memory (even if you don't think of it in an object oriented way)- it is a position in memory where the data structure for that "element" is.

So, if the passed value is an object like an array, or other element that has attributes and names, what you get inside the function is the same object. If you change it's attributes (like in the p.n = p.n + 1 example above), since the "p" object is the same inside and ouside the function.

Now, when you do: p = p + 1, your original object, referenced by the name p is replaced for another object - one that is created in the expression, and which has the value of p + 1. The name p inside the function points to another object, in another memory location entirely, and the original one, passed to the function, and reference out of it with the name x is left untouched.

This mechanism is exactly the same that happens in the Python language, for example.

Upvotes: 1

Related Questions