这个语法是如何工作的?

发布于 2024-12-09 08:51:31 字数 382 浏览 1 评论 0原文

erase: function(item){
        for (var i = this.length; i--;) {
            if (this[i] === item) this.splice(i, 1);
        }
        return this;
    }

这个 (var i = this.length; i--;) 语法结构如何工作?

应该分为三个部分吗?在这种情况下是什么?

来自此处

erase: function(item){
        for (var i = this.length; i--;) {
            if (this[i] === item) this.splice(i, 1);
        }
        return this;
    }

How would this (var i = this.length; i--;) syntactic construct work?

Should there be three sections? What is it in this case?

from here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2024-12-16 08:51:31

类 C 语言中的 for 循环由三部分组成:

for (初始化器 ; 条件 ; 更新) { ... }


然后,循环将转换为以下(大致)等效的循环(存在差异,但此处不相关):

初始化;
while条件{
    ...
    更新;
<代码>}

因此初始化程序部分首先运行;那么只要条件保持为真,循环就会运行,并且在每次迭代结束时更新部分运行。它不一定是增量,但最常见的形式如下所示:

for (var i = 0; i < something; i++) { ... }

但是您可以在这些部分中写入您想要的任何内容,如果您在循环中执行的任何操作足以使循环正确运行,您也可以将它们留空,并且终止。因此,在您的情况下,由于 i-- 是一个也更新 i 的表达式,因此循环仅结合 conditionupdate< /em> 变为 1 并倒计时。

A for loop in C-like languages consists of three parts:

for (initializer ; condition ; update) { ... }

The loop then gets transformed into the following (roughly) equivalent loop (there are differences, but not relevant here):

initializer;
while (condition) {
    ...
    update;
}

So the initializer part runs first; then the loop runs as long as the condition remains true and at the end of each iteration the update part runs. It doesn't have to be an increment but the most common form looks like this:

for (var i = 0; i < something; i++) { ... }

But you can write whatever you want into those parts and you can also leave them blank if whatever you do within the loop suffices for the loop to run correctly and terminate. So in your case, since i-- is an expression that also updates i, the loop just combines both condition and update into one and counts down.

雨巷深深 2024-12-16 08:51:31

它将 i 初始化为 this.length,并使用条件检查来减少它。当它达到零(这是一个假值)时,循环就会停止。

但请注意,因为您在检查中递减,所以循环从 this.length - 1 开始,到 0 结束。所以它相当于:

(var i = this.length - 1; i >= 0; i--)

但是请不要使用这种语法,因为它的行为令人困惑 - 正如您可能已经注意到的那样。

It initializes i to this.length, and uses the condition check to also decrement it. When it gets to zero, which is a falsy value, the loop stops.

But note that because you decrement in the check, the loop starts at this.length - 1 and ends at 0. So it's equivalent to:

(var i = this.length - 1; i >= 0; i--)

But please refrain from using this kind of syntax, as its behavior is confusing - as you might have noticed.

清浅ˋ旧时光 2024-12-16 08:51:31

在某个时刻,i 将达到 0(这是一个假值),从而结束循环。

更具体地说,经过几次迭代后:

  • 它将检查 i 并得到 1
  • i 将递减并变为 0
  • 循环将以 i == 0 运行
  • 它将检查 i 并得到 0
  • i 将递减并变为 < code>-1
  • 循环结束

At some point, i will reach 0, which is a false value, and this ends the loop.

More specifically, after several iterations:

  • It will check i and get 1
  • i will be decremented and become 0
  • The loop will run with i == 0
  • It will check i and get 0
  • i will be decremented and become -1
  • The loop will end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文