const 、 let 、 var 和 reference 有问题

Have a issue with the const , let , var and reference

我正在做一些 JavaScript 编码练习,我想复制图表

/**
 * // Definition for a Node.
 * function Node(val, neighbors) {
 *    this.val = val === undefined ? 0 : val;
 *    this.neighbors = neighbors === undefined ? [] : neighbors;
 * };
 */

/**
 * @param {Node} node
 * @return {Node}
 */
var cloneGraph = function(node) {
    
    visited = {};
    return recursion(node);
    
    function recursion(node){
        
        if (!node) return null;
        if (visited[node.val]) return visited[node.val];
        
        // clone node and clone edge
        // **I forgot the let, var, const variable declaration here**
        root = new Node(node.val);
        visited[root.val] = root;
        
        if (!node.neighbors) return root;
        for (let i = 0; i < node.neighbors.length; i++) {
            root.neighbors.push(recursion(node.neighbors[i]));
        }
        return root;
    }
};

这是我的代码。我忘记在初始化新节点时添加变量声明constletvar。但是,这里的引用似乎不正确。

错误:You must return the copy of the given node as a reference to the cloned graph..

任何人都可以帮助我理解,为什么 const var 声明在这里很重要?对于这种编码,varletconst可以通过,但是,var说是全局的,constlet好像是是块级别吗?谢谢。

然而,当使用dc打击时,我们可以得到与对象相同的输出。

    d = new Node(node.val)
    console.log(d)
    
    const c = new Node(node.val)
    console.log(c)

因为root已经被声明为全局变量并且在每个递归循环中被改变,在这一行中: root.neighbors.push(recursion(node.neighbors[i])); root不会是当前循环中的当前节点,而是它被分配到的最后一个引用。

让我们用一个简单的图表来证明它:

const oldGraph = [{
  val: 1,
  neighbors: [{
    val: 2
  }, {
    val: 3
  }]
}];

所以 运行 cloneGraph(oldGraph[0]),值 2 完成后 return 检查 !node.neighbors 后,递归回到行 root.neighbors.push(recursion(node.neighbors[i]));。现在 root 是节点 2,因为它已在第二次递归中分配给新节点 val 2,而不是您预期的节点 1 的根。

varletconst 的变量声明在这种情况下有所帮助,因为它们将 root 范围保留在当前递归中,每次迭代都会创建一个新根目录,未更改任何其他引用。

在这里测试:

function Node(node) {
  this.val = node.val;
  this.neighbors = [];
}

var cloneGraph = function(node) {
    
    visited = {};
    return recursion(node);
    
    function recursion(node){
        
        if (!node) return null;
        if (visited[node.val]) return visited[node.val];
        
        // clone node and clone edge
        // **I forgot the let, var, const variable declaration here**
        let root = new Node(node);
        visited[root.val] = root;
        
        if (!node.neighbors) return root;
        for (let i = 0; i < node.neighbors.length; i++) {
            root.neighbors.push(recursion(node.neighbors[i]));
        }
        return root;
    }
};

oldGraph = [{
  val: 1,
  neighbors: [{
    val: 2
  }, {
    val: 3
  }]
}];

console.log(cloneGraph(oldGraph[0]));