admin管理员组文章数量:1333656
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
3 Answers
Reset to default 6The 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 (Turns out the OP did use the var
keyword and the error goes away.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;
本文标签: javascriptJSHint expected an assignment or function call for multiple assignmentStack Overflow
版权声明:本文标题:javascript - JSHint expected an assignment or function call for multiple assignment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742357422a2459694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论