数组在 javascript 中通过引用传递。
不,他们不是。 ECMAScript/JavaScript 是严格按值传递的。 (更准确地说,共享调用,这是传值的一种特殊情况。)
这意味着数组是可变的,可以在被调用的函数中更改。
不,不是这个意思。它的意思正是它所说的:调用者范围内的对数组的引用作为参数传递给函数,而不是值。
array 是否可变与按引用传递与按值传递无关。 ECMAScript 不是纯粹的函数式语言,大多数对象都可以变异。 Numbers、Symbols 和 Strings 是一个例外。
为什么我无法在被调用函数中重新初始化数组?
您正在尝试修改调用方范围内的引用。这仅适用于传递引用。 ECMAScript 不是通过引用传递的,谁告诉你那是完全错误的。
python 也是如此
在这方面,Python 的行为与 ECMAScript 相同,是的,也是按值传递。
您的困惑源于您错误地认为 ECMAScript/JavaScript 是通过引用传递的,而实际上并非如此。
ECMAScript 使用按值传递,或者更准确地说,是按值传递的一种特殊情况,其中传递的值始终 是一个指针。这种特殊情况有时也称为 call-by-sharing、call-by-object-sharing 或 call-by-object。
Java(用于对象)、C#(默认情况下用于引用类型)、Smalltalk、Python、Ruby 以及几乎所有创建的面向对象语言都使用相同的约定。
注意:某些类型(例如Numbers)实际上是通过值直接传递的,而不是通过中间指针传递的。但是,由于它们是不可变的,在这种情况下,按值传递和按对象共享之间没有可观察到的行为差异,因此您可以通过简单地处理所有内容来大大简化您的心智模型作为按对象共享调用。只需将这些特殊情况解释为您无需担心的内部编译器优化即可。
这是一个简单的示例,您可以运行它来确定 ECMAScript(或任何其他语言,在您翻译后)的参数传递约定:
function isEcmascriptPassByValue(foo) {
foo.push('More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value!');
foo = 'No, ECMAScript is pass-by-reference.';
return;
}
var bar = ['Yes, of course, ECMAScript *is* pass-by-value!'];
isEcmascriptPassByValue(bar);
console.log(bar);
// Yes, of course, ECMAScript *is* pass-by-value!,
// More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value!
def is_python_pass_by_value(foo):
foo[0] = 'More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value!'
foo = ['Python is not pass-by-reference.']
quux = ['Yes, of course, Python *is* pass-by-value!']
is_python_pass_by_value(quux)
print(quux[0])
# More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value!
如果你熟悉 C#,这是了解值类型和引用类型的值传递和引用传递之间的区别的一个很好的方法,因为 C# 支持所有 4 种组合:值类型的值(“传统的值传递”),引用类型的值传递(共享调用、对象调用、对象共享调用,如 ECMAScript 中)、传递-引用类型的按引用,值类型的按引用。
(实际上,即使您不了解 C#,也并不难理解。)
// In C#, struct defines a value type, class defines a reference type
struct MutableCell
{
public string value;
}
class Program
{
// the ref keyword means pass-by-reference, otherwise it's pass-by-value
// You must explicitly request pass-by-reference both at the definition and the call
static void IsCSharpPassByValue(string[] foo, MutableCell bar, ref string baz, ref MutableCell qux)
{
foo[0] = "More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value.";
foo = new string[] { "C# is not pass-by-reference." };
bar.value = "For value types, it is *not* call-by-sharing.";
bar = new MutableCell { value = "And also not pass-by-reference." };
baz = "It also supports pass-by-reference if explicitly requested.";
qux = new MutableCell { value = "Pass-by-reference is supported for value types as well." };
}
static void Main(string[] args)
{
var quux = new string[] { "Yes, of course, C# *is* pass-by-value!" };
var corge = new MutableCell { value = "For value types it is pure pass-by-value." };
var grault = "This string will vanish because of pass-by-reference.";
var garply = new MutableCell { value = "This string will vanish because of pass-by-reference." };
// the first two are passed by value, the other two by reference
IsCSharpPassByValue(quux, corge, ref grault, ref garply);
Console.WriteLine(quux[0]);
// More precisely, for reference types it is call-by-object-sharing, which is a special case of pass-by-value.
Console.WriteLine(corge.value);
// For value types it is pure pass-by-value.
Console.WriteLine(grault);
// It also supports pass-by-reference if explicitly requested.
Console.WriteLine(garply.value);
// Pass-by-reference is supported for value types as well.
}
}