The “unexpected ++” error in jslint [duplicate]

醉酒当歌 提交于 2019-11-26 23:57:09

问题


This question already has an answer here:

  • Why avoid increment (“++”) and decrement (“--”) operators in JavaScript? 17 answers

What is the best practice for that then?

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

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];
        }
    }
  }

回答1:


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




回答2:


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




回答3:


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.

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.

This could easily be solved by doing

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

Now this cannot be mistaken.




回答4:


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.

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 ++i work, and removes the need to apply quite so many preference rules.




回答5:


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 + 1 where you explicitly reassign the variable with no side effects and then use it.

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




回答6:


JSLint friendly loop

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



回答7:


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



回答8:


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

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


来源:https://stackoverflow.com/questions/3000276/the-unexpected-error-in-jslint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!