简体   繁体   English

如何理解这个JavaScript function的流程

[英]How to understand the flow of this JavaScript function

I'm a beginner who is learning JavaScript, after HTML/CSS.我是在 HTML/CSS 之后学习 JavaScript 的初学者。 I'm at the very beginning of the book of Head First, struggling to understand this function.我在 Head First 这本书的最开始,努力理解这个 function。

How does this function work in every step?这个 function 在每一步中是如何工作的? What happens starting from thingamajig(5)?从thingamajig(5) 开始会发生什么?

 function clunk(times) { var num = times; while (num > 0) { display("clunck"); num = num - 1; } } function thingamajig(size) { var facky = 1; clunkCounter = 0; if (size == 0) { display("clanck"); } else if (size == 1) { display("thunk"); } else { while (size > 1) { facky = facky * size; size = size - 1; } clunk(facky); } } function display(output) { console.log(output); clunkCounter = clunkCounter + 1; } var clunkCounter = 0; thingamajig(5); console.log(clunkCounter);

Here's what will happen when we run this:当我们运行它时会发生以下情况:

  • starting from the top, we define three different functions: clunk , thingamajig and display从顶部开始,我们定义了三个不同的函数: clunkthingamajigdisplay

  • then we initialize a variable called clunkCounter and assign to it the number 0然后我们初始化一个名为clunkCounter的变量并为其分配数字0

  • then we call the thingamajig function, passing in the argument 5 for the size parameter然后我们调用thingamajig function,传入参数5作为size参数

  • in thingamajig , we'll enter the else branch, and we'll end up going through the while loop 4 times, so we're effectively doing facky = 1 * 5 * 4 * 3 * 2 , so facky ends up with a value of 120thingamajig中,我们将进入else分支,我们最终会经历 4 次while循环,所以我们实际上是在执行facky = 1 * 5 * 4 * 3 * 2 ,所以facky最终得到一个值120

  • then we call clunk(120)然后我们调用clunk(120)

  • so we'll call display("clunk") 120 times所以我们会调用display("clunk") 120 次

  • display just logs "clunk" , and as a side-effect increments the clunkCounter , to record how many times we've done thisdisplay日志"clunk" ,并作为副作用增加clunkCounter ,以记录我们这样做了多少次

  • then finally we log out clunkCounter , which will be 120然后最后我们注销clunkCounter ,这将是120

Why would we want to do this?我们为什么要这样做? I don't know.我不知道。 It's a very contrived example which demonstrates how to use if/else conditionals and incrementing variables.这是一个非常人为的示例,它演示了如何使用 if/else 条件和递增变量。 I wouldn't worry too much about what it all "means".我不会太担心这一切“意味着”什么。 If you haven't already, try running it in the browser console, and messing around to see what happens if you change the value you pass in to thingamajig .如果您还没有,请尝试在浏览器控制台中运行它,并四处看看如果您更改传递给thingamajig的值会发生什么。

Edit: Very well explained.编辑:很好解释。 Just to add a little, its calculating the Factorial of a number and printing its value at the end.只是补充一点,它计算一个数字的阶乘并在最后打印它的值。

it starts with thingamajig(5);它以thingamajig(5);

function thingamajig(size) {
var facky = 1;
clunkCounter = 0;
if (size == 0) {
    display("clanck");
} else if (size == 1) {
    display("thunk");
} else {
    while (size > 1) {
        facky = facky * size;
        size = size - 1;
    }
    clunk(facky);
}

} }

it takes "5" as parameter which means the "size" variable is 5 and starts to check the conditions in if blocks.它以“5”作为参数,这意味着“size”变量为 5,并开始检查 if 块中的条件。

now lets see.现在让我们看看。 the size is 5 so it will skip the first 2 part of the if block大小为 5,因此它将跳过 if 块的前 2 部分

`if (size == 0) {
display("clanck");
} else if (size == 1) {
display("thunk");
}`

and execute the else part并执行 else 部分

else {
        while (size > 1) {
            facky = facky * size;
            size = size - 1;
        }
        clunk(facky);
    }

this while loop will work until the size > 1 that means the size should be 1 to break the loop.这个 while 循环将一直工作到 size > 1,这意味着 size 应该为 1 才能中断循环。 there are some calculations in the while loop. while循环中有一些计算。

the "facky" variable changes but in the end the "size" variable will be "1" and the "facky" will be 96 “facky”变量发生变化,但最终“size”变量将为“1”,“facky”变量为 96

when the "while loop" ends it will call clunk(facky);当“while 循环”结束时,它将调用clunk(facky);

that means这意味着

`function clunk(96) {
    var num = 96;
    while (96 > 0) {
        display("clunck");
        num = num - 1;
    }
}`

this function will call "display" function 96 times.此 function 将调用“显示”function 96 次。 and display function will console log "clunck" word for 96 times in the console.并显示 function 将在控制台中控制台记录“clunck”字 96 次。

in the end the clucnkCounter will be consoled log.最后 clucnkCounter 将被控制台记录。

I hope i understand the question right.我希望我能正确理解这个问题。 because answering this question in writing is hard.因为以书面形式回答这个问题很难。

  • in thingamajig() function the value is passed 5.在 thingamajig() function 中,该值被传递 5。
  • and its is checked whether its matches with 0 or 1, then its goes to else block, here is while loop.并检查其是否与 0 或 1 匹配,然后转到 else 块,这里是 while 循环。 facky variable initial value is 1 but here its value is assign again so its become 5, and size is decremented so become 4 faky 变量的初始值为 1,但在这里它的值被再次赋值,所以它变成了 5,并且大小减少了,所以变成了 4
  • again in while its greater than 1, again 5*4=20, facky become 20.再次在其大于 1 时,再次 5*4=20,faky 变为 20。
  • while continue until size value is 1. when size value is 1, facky become 120.同时继续直到 size 值为 1。当 size 值为 1 时,facky 变为 120。
  • (5)x(4)x(3)x(2) = 120 (5)x(4)x(3)x(2) = 120
  • now clank function is called and times = 120现在叮叮当当 function 被调用,次数 = 120
  • here num = times, so num = 120这里 num = 次,所以 num = 120
  • while num > 0, it call another function display, which console.log(output).当 num > 0 时,它调用另一个 function 显示,console.log(输出)。 And here output = "clunk".这里 output = "clunk"。
  • And increase the value of clunkCounter by 1.并将 clunkCounter 的值增加 1。
  • Its keep on repeating until num = 0, and making clunkCounter = 120它不断重复直到 num = 0,并使 clunkCounter = 120

-The code starts executing from the else code block in the function thingamajig(size) , since the if and else if statement are false. - 代码从 function thingamajig(size)中的 else 代码块开始执行,因为 if 和 else if 语句为假。

else {
      while (size > 1) {
      facky = facky * size;
      size = size - 1; }
      clunk(facky); }
      }

In the else statement we have a while loop with a condition (size > 1) , size is 5 inserted as an argument for the size parameter when invoked thingamajig(5) ;.在 else 语句中,我们有一个带有条件(size > 1)的 while 循环,在调用thingamajig(5)时, size 是 5 作为 size 参数的参数插入;。

The code loops till size = 1, when the condition becomes false .当条件变为false时,代码循环直到 size = 1。

  1. LOOP 1, while size = 5, facky is = 1, facky = 1 * 5 = 5, size - 1 =size becomes 4. LOOP 1,size = 5,facky is = 1,facky = 1 * 5 = 5,size - 1 =size 变为 4。

  2. LOOP 2, while size = 4, facky = 5, facky = 4 * 5 = 20, size - 1 = size becomes 3.循环 2,当 size = 4,facky = 5,facky = 4 * 5 = 20,size - 1 = size 变为 3。

  3. LOOP 3, while size = 3, facky = 20, facky = 3 * 20 = 60, size - 1 = size becomes 2. LOOP 3,当 size = 3,facky = 20,facky = 3 * 20 = 60,size - 1 = size 变为 2。

  4. LOOP 4, while size = 2, facky = 60, facky = 3 * 60 = 120, size - 1 = size becomes 1.循环 4,当 size = 2,facky = 60,facky = 3 * 60 = 120,size - 1 = size 变为 1。

  • Before loop stops The clunk() function is invoked and facky is passed as an argument to the times parameter, the function clunk starts executing.在循环停止之前调用clunk() function 并将 facky 作为参数传递给 times 参数,function clunk 开始执行。

    function clunk(times) { var num = times; function clunk(次) { var num = times; while (num > 0) { display("clunk"); while (num > 0) { display("clunk"); num = num - 1;数 = 数 - 1; } } } }

Here, times = facky = 120 = num, The while loop starts executing until num = 0 when the condition becomes false , in this loop the display() function is invoked with the string 'clunk' as an argument.这里,times = facky = 120 = num,while 循环开始执行,直到条件变为false时 num = 0,在此循环中,使用字符串 'clunk' 作为参数调用display() function。

function display(output) {
         console.log(output); 
         clunkCounter = clunkCounter + 1;
      }
  • The display('clunk') function starts executing. display('clunk') function 开始执行。
  • 'clunk' string is copied into the output parameter and it is logged into the console & clunkCounter variable increments. 'clunk' 字符串被复制到 output 参数中,并记录到控制台和 clunkCounter 变量增量中。 -The both continues logging string 'clunk' & incrementing clunckCounter, until num = 0, as num decrements from 120 till it gets to 0. - 两者都继续记录字符串 'clunk' 并递增 clunckCounter,直到 num = 0,因为 num 从 120 递减直到它变为 0。

Results结果

console.log(output);

  • 'clunk' strings logs into the console 120 times. 'clunk' 字符串登录控制台 120 次。

console.log(clunkCounter); -clunkCounter increments till its 120 so 120 is logged into the console. -clunkCounter 递增到 120,因此 120 被记录到控制台。

The main thing to understand for those that still don't get it (like I did not understand when I first looked at this) is that "facky" changes values every time the while loop runs.对于那些仍然不明白的人(就像我第一次看到这个时我不明白),要理解的主要事情是每次 while 循环运行时“faky”都会改变值。 So if you start with thingamajig(5), facky=5.所以如果你从 thingamajig(5) 开始,facky=5。 But then size becomes "size=4" which makes it so you go through the while loop again.但是随后大小变为“size = 4”,这使得您再次通过while循环 go 。 THIS TIME facky is going to be "facky=5x4" and therefore it is "facky=20".这一次 facky 将是“facky=5x4”,因此它是“facky=20”。 Then you go through the while loop again with "size=3" which makes it "facky=20x3" and there for it is "facky=60".然后你 go 通过 while 循环再次使用“size = 3”使其成为“facky = 20x3”并且它是“facky = 60”。 One last time through the while loop and you get "facky=60x2" and therefore it is "facky=160".最后一次通过while循环,你得到“facky = 60x2”,因此它是“facky = 160”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM