在 JavaScript 中通过引用返回?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10956970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning by reference in JavaScript?
提问by MTVS
Is it true that in JavaScript functions return objects other than Boolean and Numbers by reference?
在 JavaScript 中,函数是否通过引用返回布尔值和数字以外的对象?
How is this possible when those objects are destroyed when the function they belong to terminates?
当它们所属的函数终止时这些对象被销毁,这怎么可能?
回答by Kendall Frey
Objects are not destroyed until allreferences 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.
作为旁注,它如何返回并不重要,因为函数在返回后无论如何都不能使用该对象。
回答by zerkms
Is it true that in JavaScript functions return objects other than Boolean and Numbers by reference?
在 JavaScript 中,函数是否通过引用返回布尔值和数字以外的对象?
It is true, objects in JavaScript are alwayspassed by reference
确实,JavaScript 中的对象总是通过引用传递
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.
只有引用被销毁,而不是值本身。只要没有引用剩余,该对象就是垃圾收集的候选对象。
回答by Nathan MacInnes
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.
其中 undefined 意味着它已被复制而不是通过引用传递。请注意,字符串也不是通过引用传递的。
回答by Alex Kudryashev
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.
底线:您可以对对象的属性执行任何操作,但不能对对象本身执行任何操作。对参数的任何赋值都使它成为不同的对象并与初始对象取消链接。