我正在使用 JSHint 检查我的 JS 文件以查找错误。
我刚收到这个警告:
Expected an assignment or function call and instead saw an expression.
(mandatEngagementExists)
? $('.mandat-type[value=1]').prop('checked', false).prop('disabled', true)
: $('.mandat-type[value=1]').prop('disabled', false);
mandatEngagementExists
是一个 bool 值。
我所有的 JS 都处于严格模式。
这段代码有效,所以我想知道 JSHint 是否有点过于严格,或者我是否应该切换到经典的 if/else。
直接在 .prop("property", boolean)
中使用 bool 值
$(el).prop('checked', myBoolean);
使用返回
function myFn() {
/* Code here */
return myBoolean ? $(el).doThis() : $(el).doThat();
/* !!!!!!!!! >>> Cannot have more code here since `return` was used */
}
使用 IIFE
function myFn() {
/* Code here */
(function(){
return myBoolean? $(el).doThis() : $(el).doThat();
}());
/* Code here */
}
使用var
var someVar;
function myFn() {
/* Code here */
someVar = myBoolean? $(el).doThis() : $(el).doThat();
/* Code here */
}
使用if else
function myFn() {
/* Code here */
if(myBoolean) {
$(el).doThis();
} else {
$(el).doThat();
}
/* Code here */
}