Javascript为什么我不知道名字已经存在错误 [英] Javascript why am I not getting name already exist error

查看:75
本文介绍了Javascript为什么我不知道名字已经存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解Javascript和递归编程的行为。

I was trying to comprehend the behaviour of Javascript and recursive programming.

Npw,我是一个初学者,所以我需要了解为什么我已经被声明为错误以及什么时候不知道已经被声明为错误

Npw, I am sort of beginner so I need to understand why do I get already have been declared error and when I don't get already have been declared error

考虑此代码,我试图理解其执行方式。

Consider this code, I was trying to understand how this will execute..

 let company = { // the same object, compressed for brevity
      sales: [{name: 'John', salary: 1000}, {name: 'Alice', salary: 600 }],
      development: {
        sites: [{name: 'Peter', salary: 2000}, {name: 'Alex', salary: 1800 }],
        internals: [{name: 'Jack', salary: 1300}]
      }
    };
    
    // The function to do the job
    function sumSalaries(department) {
      if (Array.isArray(department)) {
        return department.reduce((prev, current) => prev + current.salary, 0);
      } else { // case (2)
        let sum = 0;
        for (let subdep of Object.values(department)) {
          sum = sum + sumSalaries(subdep); 
        }
        return sum;
      }
    }
    
   console.log(sumSalaries(company));// 6700

中断上述代码的执行(如果我理解不正确,则正确)

Breaking the execution of the above code (correct if I am understanding this wrong)


  1. 我们将公司作为sumSalaries的参数传递

  2. 在sumSalaries内部,我们正在检查它是否为数组

最初不是因为company是上面的对象,所以返回一个数组。

Intitally it is not an array since company is an object above.


  1. 我们将通过let sum = 0的声明传递条件

  2. object.values将给我们两个数组(基于公司)

  3. let subdep的Object.values(department ,我们将获得
    [{…},{…}],其中包含以下对象{name: John,薪金:1000}

  4. 在这里我们的总和= 0,我们将把我们的总和加上我们传给 sumSalaries(subdep);

  5. 现在,因为我们正在传递数组,它将进入以下函数

  1. We will pass it else condition with declaration of let sum = 0
  2. object.values will give us two arrays (based from company)
  3. In the first iteration of let subdep of Object.values(department, we will get [{…}, {…}] inside which we have following object {name: "John", salary: 1000}
  4. Here our sum = 0, we will add our sum with return of what we passing to sumSalaries(subdep);
  5. Now since we are passing the array, it will go to following function

if(Array.isArray(department)){
return department.reduce((prev,current)=> prev + current.salary,0);
}

if (Array.isArray(department)) { return department.reduce((prev, current) => prev + current.salary, 0); }

问题+问题
在完成十三点时,我意识到第二个声明让sum = 0

Problem + Question While, I was completing the thirteen point, I realised that our second declaration have let sum = 0

所以,有两件事


  1. 由于我们有 let sum = 0 ,所以不应有错误指出让sum 已经存在

  1. Since we have let sum = 0, shouldn't there be an error stating that let sum already exsist

无论哪种方式,我们正在做sum = 0,这是否意味着我们以前的值( 1600求和数组中的消失了(换句话说,重置为零了吗?)

Either way, we are doing sum = 0, does that mean our previous value (1600) in the sum array is gone (or in other words reset to zero?


推荐答案

let 是块范围的,这意味着可以在多个块中包含多个声明。每次调用 sumSalaries()时, sum 都将重置为零。它不需要记住以前的调用,因为如果找到此调用,它将返回总和,随着递归展开,该调用将添加到调用递归函数的父级中。

let is block scoped, which means having multiple declarations in multiple blocks is okay. Each time you call sumSalaries() the sum will be reset to zero. It doesn't need to remember previous calls because it will return the sum if finds for this call which, as the recursion unwinds, will be added to the parent who called the recursive function.

通过战略性地放置,调用 console.log()或使用调试器来观察递归可能具有指导意义。例如,您可以看到总计为:

It can be instructive to watch the recursion with strategically placed, console.log() calls or by using a debugger. For example you can watch the sum add up with:

 let company = { // the same object, compressed for brevity
      sales: [{name: 'John', salary: 1000}, {name: 'Alice', salary: 600 }],
      development: {
        sites: [{name: 'Peter', salary: 2000}, {name: 'Alex', salary: 1800 }],
        internals: [{name: 'Jack', salary: 1300}]
      }
    };
    
    // The function to do the job
    function sumSalaries(department) {
      if (Array.isArray(department)) {
        return department.reduce((prev, current) => prev + current.salary, 0);
      } else { // case (2)
        console.log("starting new object")
        let sum = 0;
        for (let subdep of Object.values(department)) {
          let subsum =  sumSalaries(subdep)
          console.log("subsum = ", subsum)
          sum = sum + subsum; 
        }
        console.log("current sum:", sum)
        return sum;
      }
    }
    
   console.log(sumSalaries(company));// 6700

这篇关于Javascript为什么我不知道名字已经存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆