奇怪的TypeScript语法

huangapple go评论86阅读模式
英文:

Strange TypeScript Syntax

问题

我正在为你翻译以下内容:

我正在查看一个用于计算平均运行时间的TypeScript函数,遇到了一些我以前没见过的奇怪语法:

func averageRuntimeInSeconds(runs []Run) float64 {
    var totalTime int
    var failedRuns int
    for _, run := range runs {
        if run.Failed {
            failedRuns++
        } else {
            totalTime += run.Time
        }
    }

    averageRuntime := float64(totalTime) / float64(len(runs) - failedRuns) / 1000
    return averageRuntime
}

第4行的 := 符号是什么意思?
还有,在代码的第4行,for循环的语法对我来说看起来很奇怪。没有括号。那是什么情况?那是什么类型的for循环?

最后,range 关键字是什么意思?

英文:

I was looking at a TypeScript function to calculate an average run time and I encountered some strange syntax I haven't seen before:

    func averageRuntimeInSeconds(runs []Run) float64 {
    var totalTime int
    var failedRuns int
    for _, run := range runs {
        if run.Failed {
            failedRuns++
        } else {
            totalTime += run.Time
        }
    }

    averageRuntime := float64(totalTime) / float64(len(runs) - failedRuns) / 1000
    return averageRuntime
}

What does the

> :=

symbol mean on the 4th line?
Also on the 4th line of that code, the syntax for the for loop looks very strange to me. There are no parentheses. What is going on there? What kind of for loop is that?

And lastly, what does the range keyword do?

答案1

得分: 0

如RAZAFINARIVO在我的问题评论中指出的那样,该函数实际上是来自Golang语言,而不是Typescript。我错误地认为它是Typescript。谢谢RAZAFINARIVO!

英文:

As RAZAFINARIVO has pointed out in the comments of my question, the function is actually from the Golang language, not Typescript. I was mistaken in thinking it was Typescript. Thanks RAZAFINARIVO!

huangapple
  • 本文由 发表于 2021年10月29日 00:21:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69757680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定