Javascript jslint 中的“意外 ++”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3000276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:51:00  来源:igfitidea点击:

The "unexpected ++" error in jslint

javascriptjslint

提问by KdgDev

What isthe best practice for that then?

什么对于那么最好的做法是什么?

Jslint explains that it "adds confusion". I don't see it really...

Jslint 解释说它“增加了混乱”。真没看出来...

EDIT: The code, as requested:

编辑:代码,按要求:

  var all,l,elements,e;
  all = inElement.getElementsByTagName('*');
  l = all.length;
  elements = [];
  for (e = 0; e < l; (e++))
  {
    if (findIn)
    {
        if (all[e].className.indexOf(className) > 0)
        {
            elements[elements.length] = all[e];
        }
    } else {
        if (all[e].className === className)
        {
            elements[elements.length] = all[e];
        }
    }
  }

采纳答案by unomi

Use i += 1instead, if you want to follow jslint's advice.

i += 1如果您想遵循 jslint 的建议,请改用。

回答by Rico Sonntag

Just add /*jslint plusplus: true */in front of your javascript file.

只需/*jslint plusplus: true */在您的 javascript 文件前面添加。

回答by Sean Kinsey

To avoid confusion, and possible problems when using minifiers, always wrap parens around the operator and its operand when used together with the same (+ or -).

为了避免混淆和使用缩小器时可能出现的问题,当与相同的(+ 或 -)一起使用时,始终将括号括在运算符及其操作数周围。

var i = 0, j = 0;
alert(i++ +j);

This adds i and j (and increments i as a side effect) resulting in 0 being alerted.

这会添加 i 和 j(并增加 i 作为副作用)导致 0 被警告。

But what is someone comes along and moves the space?

但是什么是有人出现并移动空间?

var i = 0, j = 0;
alert(i+ ++j);

Now this first increments j, and then adds i to the new value of j, resulting in 1 being alerted.

现在这首先增加 j,然后将 i 添加到 j 的新值,导致 1 被警告。

This could easily be solved by doing

这可以通过执行轻松解决

var i = 0, j = 0;
alert((i++) +j); 

Now this cannot be mistaken.

现在这不能错。

回答by Samir Talwar

Personally, I prefer to put statements such as i++on a line by themselves. Including them as part of a larger statement can cause confusion for those who aren't sure what the line's supposed to be doing.

就个人而言,我更喜欢将诸如此类的语句单独放在i++一行中。将它们作为更大声明的一部分可能会导致那些不确定该行应该做什么的人感到困惑。

For example, instead of:

例如,而不是:

value = func(i++ * 3);

I would do this:

我会这样做:

value = func(i * 3);
i++;

It also means people don't have to remember how i++and ++iwork, and removes the need to apply quite so many preference rules.

这也意味着人们不必记住如何i++++i工作,并且不需要应用如此多的偏好规则。

回答by benzonico

The real problem of the ++operator is that it is an operator with side effects and thus it is totally opposed to the principle of functional programming.

++操作符的真正问题在于它是一个有副作用的操作符,因此完全违背了函数式编程的原则。

The "functional" way to implement i++would be i = i + 1where you explicitly reassign the variable with no side effects and then use it.

功能性”的实现i++方式是i = i + 1您明确地重新分配变量而没有副作用,然后使用它。

The possibility of confusion is that ++does two things by adding a value AND reassigning it to the variable.

混淆的可能性是++通过添加值并将其重新分配给变量来做两件事。

回答by GeographicPerspective

JSLint friendly loop

JSLint 友好循环

for (i = 0; i < 10; i += 1) {
    //Do somthing
}

回答by miketheprogrammer

Please note that the ++ operator depends on position with respect to the prior/next variable and the newline / semicolon to determine order of operations.

请注意,++ 运算符取决于相对于前一个/下一个变量和换行符/分号的位置来确定操作顺序。

var a = 1;
var b = a++;
console.log(b); // b = 1
console.log(a); // a = 2

var a = 1;
var b = ++a;
console.log(b); // b = 2
console.log(a); // a = 2

回答by Paul Scheltema

There is something called a pre-increment: ++i and a post-increment i++ and there is a difference:

有一种叫做 pre-increment: ++i 和 post-increment i++ 的东西,有区别:

var i = 9;
alert(++i); //-> alerts 10

var j = 9;
alert(j++); //-> alerts 9
alert(j);   //-> alerts 10 now, as expected

var k = 9;
alert((k++)); //-> still alerts 9 even with extra parentheses