LESS mixin中的递归循环:为什么计数器递减工作,而whyle增量不起作用?

时间:2014-07-02 13:37:40

标签: loops recursion less mixins

作为一个新手,我对解析器在某段特定代码上的行为感到困惑。

我正在学习一门课程,教我在LESS文件中输入此代码并解析它:

.lusGuardedMixin (@index) when (@index > 0) {
    @waarde: -((38*@index)-38);

    .m_item@{index} {
        background-position: ~"@{waarde}px" 0;

        &:hover {
            background-position: ~"@{waarde}px" -39px;
        }

        & span {
            .menuspan(~"bgcolor_@{index}", ~"color_@{index}", ~"tshadow_@{index}")
        }
    }
    .lusGuardedMixin(@index + 1);

}
.lusGuardedMixin(6) {}
.lusGuardedMixin(1);

您可以忽略" .menus" -part,它引用一段与此无关的代码。

现在,在WinLESS中运行此代码时,它会创建一个解析器错误:" SYNTAR ERROR:undefined",然后引用我声明的行:

.lusGuardedMixin(1);

我尝试了各种各样的事情 - 比如更改起始编号 - 但是记住LESS文档(及其递归示例)时,我最终将部分代码更改为:

[...]
     .lusGuardedMixin(@index - 1);

}
.lusGuardedMixin(0) // this is no longer explicitly necessary due to the restriction "> 0", but is still here for reference of changes I did try
.lusGuardedMixin(5);

现在它有效!但我不知道为什么... 增量(@index + 1)失败的原因是什么,但减少(@index - 1)在这段代码中起作用?

注意:我还设法使用原始计数器增量来解决它,将原始循环条件when (@index > 0)替换为when (@index > 0) and (@index < 6),但是我仍然无法理解为什么原始示例赢了&# 39;工作。

提前感谢您的帮助:)

0 个答案:

没有答案
相关问题