LiveScript for循环不正常或者我有错误的语法

LiveScript for loop not behaving or I have the wrong syntax

本文关键字:有错误 语法 或者 for 循环 不正常 LiveScript      更新时间:2023-09-26

在正常情况下(香草JS)我可能会这样做-

var mystring = "foo";
for(i = 0; i < mystring.length; i++) {
    console.log(i);
}

返回[0,1,2 '

我找不到在LiveScript中产生这种行为的语法。我最接近的是这个-

 mystring = 'foo
 for i from 0 to my.length-1 // note the -1
     console.log i

编译成这个JavaScript -

var mystring, i$, to$, i;
mystring = 'foo';
for (i$ = 0, to$ = mystring.length - 1; i$ <= to$; ++i$) {
  i = i$;
  console.log(i);
}

这也返回[0,1,2]

如果我不包括-1,返回的数组是[0,1,2,3],这是预期的,因为在这种情况下,LiveScript是如何编译成JavaScript的。

是不可能得到一个纯粹的'小于'条件在LiveScript?

您要使用til,而不是to

for i from 0 til my.length

LiveScript循环