JavaScript 如何处理传递给与这个链表示例相关的函数的对象

How does JavaScript handle objects passed to functions with regards to this linked list example?

本文关键字:表示 链表 对象 函数 何处理 处理 JavaScript      更新时间:2023-09-26

我注意到我在JavaScript中不理解的行为。我知道,对于传递给函数的基元类型,它们是按值传递的。如果我们传递一个对象,它们将通过引用传递。我想做的是通过将重复的部分封装到函数中来缩短我的代码;这个函数是获取指向链表中节点的节点对象,对其值执行某些操作,然后将其移动到列表中的下一个节点。然而,事实并非如此。我在这里做了一个简单的例子:

var testObj = {};
testObj.val = 5;
testObj.next = {
    val: 7,
    next: null
} 
var temp = testObj;
console.log("Val before: ", temp.val); //prints 5 
function test(node) {
    console.log(node.val); //prints 5
    node = node.next;
    console.log(node.val); //prints 7
}
test(temp);
console.log("Val after: ", temp.val); //prints 5?? 

首先,我创建一个测试节点,其中包含一个值和一个指向列表中下一个节点的下一个字段,该节点也有一个值。我创建了另一个指向列表中第一个节点的节点(当您想要遍历列表时,这很常见,您不想失去对根节点的跟踪。

我很困惑为什么你可以做这样的事情

function test(node) { 
    node.val = 100;
}
console.log(temp.val); //prints 100

值的变化会保留;但是如果我让节点指向列表中的下一项,这种变化就不会保留。

Javascript 将值复制到您作为参数传递给的函数。

var temp = testObj; // temp is defined in this scope
console.log("Val before: ", temp.val); //prints 5 
    function test(node) { //node is defined in those scope, and is not a reference.
        console.log(node.val); //prints node
        node = node.next;
        //if you wouldve done node.value = 10, it wouldve changed for temp as value is encapsulated
        console.log(node.val); //prints node
    }
test(temp); //temp is still the same as it was defined as you didn't change it
console.log("Val after: ", temp.val); //prints temp

如果你想使参数成为对你正在传递的对象的引用,你需要将其封装在另一个对象中,即如果你在函数中再次设置参数,对旧对象的引用就会消失。看看这个问题