Why does JSHint dislike ternaries for method calls on objects?

后端 未结 3 538
一生所求
一生所求 2021-01-18 03:05

JSHint give the following error:

Expected an assignment or function call and instead saw an expression.

For the following line o

3条回答
  •  孤街浪徒
    2021-01-18 03:36

    JSHint says about expressions, or expr:

    This option suppresses warnings about the use of expressions where normally you would expect to see assignments or function calls. Most of the time, such code is a typo. However, it is not forbidden by the spec and that's why this warning is optional.

    While JSLint says:

    An expression statement is expected to be an assignment or a function/method call or delete. All other expression statements are considered to be errors.

    AFAIK, there's no problem in doing what you're doing only that it will issue a warning because it would expect you to use an if..else statement, but you can turn this off in JSHint with:

    /*jshint expr:true */
    

提交回复
热议问题