16

Is it true that in JavaScript functions return objects other than Boolean and Numbers by reference?

How is this possible when those objects are destroyed when the function they belong to terminates?

1
  • Javascript is not C. Commented Aug 7, 2016 at 7:46

4 Answers 4

22

Objects are not destroyed until all references to them are gone and garbage collected. When the object is returned, the calling code gains a reference to it, and the object is not garbage collected.

Technically, the called function's stack frame is destroyed when it returns. The object, however, is not on the stack, but on the heap. The function's local reference to the object is on the stack, and is therefore destroyed, but the calling code's reference isn't destroyed until some time later.

As a side note, it isn't really important how it is returned, because the function can't use the object anyway after it returns.

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

1 Comment

The activation record itself may persist when the function returns, if the function affected its outer scope in such a way as to expose a reference to it. That is, if a function instantiates a function, and that function references the local scope (the activation record, or "stack frame" - which it really isn't), and that function is returned or assigned to some relatively-global variable or property, then the activation record remains available and is not collected.
8

Is it true that in JavaScript functions return objects other than Boolean and Numbers by reference?

It is true, objects in JavaScript are always passed by reference

How is it possible when those objects destroyed when the function those belong to terminates?

Only references are destroyed, not the values itself. And as long as there are no references left, the object is a candidate to be garbage-collected.

Comments

7

Two great answers there, but just thought I should add that it's easy enough to test:

function modify(arg) {
    arg.modified = true;
}
test = 4;
modify(test);
console.log(test.modified); // undefined
test = {};
modify(test);
console.log(test.modified); // true
test = "";
modify(test);
console.log(test.modified); // undefined

where undefined means it has been copied instead of being passed by reference. Note that strings aren't passed by reference either.

Comments

1

Arguments (including objects) are sent to a function by value. Properties of an argument object are available and changeable that is behaves like references. Look at the example below:

function changeObj(obj){obj.a *= 10; return true;}
function killObj(obj){obj = null; return true;}
function changeAndKillObj(obj){obj.a += 1; obj = null; return true;}
var a = {a:1, b:'test'};
console.log(a); //Object {a:1, b:'test'}
changeObj(a);
console.log(a); //Object {a:10, b:'test'}
killObj(a);
console.log(a); //Object {a:10, b:'test'}
changeAndKillObj(a); //Object {a:11, b:'test'}

Bottom line: You can do whatever you want with object's properties but not with the object itself. Any assignment to the argument makes it different object and unlink from initial object.

3 Comments

This is so wrong on so many levels! Setting local object reference to null does not affect the object itself, hence the result, which has no relation to "bottom line"
@alemjerus Setting local object reference to null does not affect the object itself This exactly what I tried to show. The value of an object is an address of (reference to) it. This value is immutable.
"Any assignment to the argument makes it different object and unlink from initial object." - check this again. Is there a default clone operation everyone was looking for for years?

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.