arrays - JavaScript : Expected and assignment or function call and instead saw an expression - Stack Overflow

I am using JSHint to ensure my JavaScript is "strict" and I'm getting the following erro

I am using JSHint to ensure my JavaScript is "strict" and I'm getting the following error:

Expected an assignment or function call and instead saw an expression

On the following code:

      var str = 'A=B|C=D'
      var data = {};
      var strArr = str.split( '|' );
      for (var i = 0; i < strArr.length; i++) {
          var a = strArr[i].split('=');
          a[1] && (data[a[0].toLowerCase()] = a[1]);  // Warning from JSHint
      } 

Any ideas why I'm getting such an error or how I can code to remove the error.

I am using JSHint to ensure my JavaScript is "strict" and I'm getting the following error:

Expected an assignment or function call and instead saw an expression

On the following code:

      var str = 'A=B|C=D'
      var data = {};
      var strArr = str.split( '|' );
      for (var i = 0; i < strArr.length; i++) {
          var a = strArr[i].split('=');
          a[1] && (data[a[0].toLowerCase()] = a[1]);  // Warning from JSHint
      } 

Any ideas why I'm getting such an error or how I can code to remove the error.

Share Improve this question edited Apr 20, 2014 at 15:29 HSG asked Apr 20, 2014 at 15:08 HSGHSG 1931 gold badge2 silver badges9 bronze badges 4
  • what are you trying to do with the last line? – attila Commented Apr 20, 2014 at 15:11
  • 2 seems to me that you are assigning (data[a[0].toLowerCase()] = a[1]) instead of paring (data[a[0].toLowerCase()] == a[1]). – Adelin Commented Apr 20, 2014 at 15:12
  • the code is trying to parse a string and convert it to an object. For example 'ABC=XYZ'. THe expected result is data.abc = xyz. – HSG Commented Apr 20, 2014 at 15:14
  • 1 The last line looks confusing, you could rewrite it more clearly anyway. – Evan Trimboli Commented Apr 20, 2014 at 18:59
Add a ment  | 

2 Answers 2

Reset to default 5

Here is a simplified version that gives the same warning:

var a, b;
a && (b = a);

Expected an assignment or function call and instead saw an expression

This means that you have an expression but do not assign the result to any variable. jshint doesn't care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.

There is another error by jslint if you care about it:

Unexpected assignment expression

This warns you that you may want to use == instead of = inside logical expressions. It's a mon error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).

Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn't be an expression.

http://jshint./docs/options/#expr - JSHINT says, Expr warnings are part of relaxing options. So, if you write /* jshint expr: true */, it won't give you the warning. But, you have to know the scope of function too. If you just type this line on top of everything, it will apply this rule globally. So, even if you made a mistake on the other lines, jshint will ignore it. So, make sure you use this wisely. Try to use if for particular function ( i mean inside one function only)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743747990a4500366.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信