最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSHint expected an assignment or function call for multiple assignment - Stack Overflow

programmeradmin0浏览0评论

I was linting some existing JavaScript and I get the error message Expected an assignment or function call and instead saw an expression. for the following.

var k, i;
k = 0, i = -1;

The assignment appears to happen correctly, so what is wrong with this that it causes the lint warning?


For a bigger picture; here is what the beginning of the function looks like that leads up to this, if it is helpful.

var subgroups = {},
    groupSums = [],
    groupIndex = d3.range(n),
    subgroupIndex = [],
    k,
    x,
    x0,
    i,
    j;

chords = [];
groups = [];

// Compute the sum.
k = 0, i = -1; while (++i < n) {
  x = 0, j = -1; while (++j < n) {
    x += matrix[i][j];
  }
  groupSums.push(x);
  subgroupIndex.push(d3.range(n));
  k += x;
}

I was linting some existing JavaScript and I get the error message Expected an assignment or function call and instead saw an expression. for the following.

var k, i;
k = 0, i = -1;

The assignment appears to happen correctly, so what is wrong with this that it causes the lint warning?


For a bigger picture; here is what the beginning of the function looks like that leads up to this, if it is helpful.

var subgroups = {},
    groupSums = [],
    groupIndex = d3.range(n),
    subgroupIndex = [],
    k,
    x,
    x0,
    i,
    j;

chords = [];
groups = [];

// Compute the sum.
k = 0, i = -1; while (++i < n) {
  x = 0, j = -1; while (++j < n) {
    x += matrix[i][j];
  }
  groupSums.push(x);
  subgroupIndex.push(d3.range(n));
  k += x;
}
Share Improve this question edited Oct 30, 2014 at 16:42 bigtunacan asked Oct 30, 2014 at 16:34 bigtunacanbigtunacan 4,9968 gold badges44 silver badges78 bronze badges 3
  • Can you post the rest of the code too? – Scimonster Commented Oct 30, 2014 at 16:35
  • I've added additional code above. This is the very beginning of a function; the variables are defined ahead of time and should hoist to my function scope appropriately. – bigtunacan Commented Oct 30, 2014 at 16:44
  • Please clarify jshint vs. jslint, I guess the former? – user663031 Commented Oct 30, 2014 at 16:50
Add a ment  | 

3 Answers 3

Reset to default 6

The ma operator creates an expression. It's an operator that evaluates the two things on the left and right, then yields as its value the one on the right. Even if the things separated by the mas are assignments, JS still views the ma operator as creating an expression.

JSLint and JSHint don't like expressions that are just sitting there not doing anything, such as

0 !== 1;

To the linters,

k = 0, i = 1;

is a forlorn expression, not doing anything.

In your particular case, it could be easier to just do

for (k = 0, i = -1; ++i < n; ) {

and be done with it, although personally I'd write

for (k = 0, i = 0; i < n; i++) {

As you probably already know, you can turn this off (examples for JSHint):

/*jshint -W030*/
/*jshint expr:true*/

Use the var keyword and the error goes away. (Turns out the OP did use the var keyword.)

Rolling two separate assignments into one expression with the ma operator like this:

k = 0, i = -1;

is legally possible in JavaScript. It is not considered good style, though. Simply use the semicolon as a statement separator:

k = 0; i = -1;

The ma operator, as per the MDN:

You can use the ma operator when you want to include multiple expressions in a location that requires a single expression.

Your situation does not fit the bill. Clever use of language features is considered harmful. Try not to do clever stuff outside of code golf petitions.

You should do it like this:

k = 0;
i = -1;
发布评论

评论列表(0)

  1. 暂无评论