在linting my Javascript中,我在我的复杂三元选项上遇到var obvious = (1 === 1) ? true : false;
// can simply become:
var obvious = (1 === 1);
警告。
我知道如何在简单的布尔表达式上解决这个问题:
const include =
(options.directory && file !== '.') ? false :
(!dotted) ? true :
(dotted && options.all) ? true :
(dotted && !implied && options.almostall) ? true :
(options.directory && file === '.') ? true :
false;
然而,在我的下面的布尔表达式中,我不知道如何正确地缩小它,而不用担心会破坏一些非常复杂的东西:
const include = !(options.directory && file !== '.') ||
(!dotted) ||
(dotted && options.all) ||
(dotted && !implied && options.almostall) ||
(options.directory && file === '.');
这个适当的速记实现是什么?
$("#topic_content_input[data-placeholder]:not([data-div-placeholder-content]):before").css({"color":"blue"});
这是对的吗?
答案 0 :(得分:4)
使用一堆链式三元运算符编写代码时,它变得更简洁,通常更不易读。
const include =
(options.directory && file !== '.') ? false :
(!dotted) ? true :
(dotted && options.all) ? true :
(dotted && !implied && options.almostall) ? true :
(options.directory && file === '.') ? true :
false;
为了解决这个问题,我将首先使用模块模式展开它:
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
if (!dotted)
return true;
if (dotted && all)
return true;
if (dotted && implied && almost)
return true;
if (directory && isDot)
return true;
return false;
}());
这可以简化。检查!dotted
后,dotted
必须为真,并且变得多余:
true && a
转换为:
a
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
if (!dotted)
return true;
if (all)
return true;
if (implied && almost)
return true;
if (directory && isDot)
return true;
return false;
}());
作为一个单独留下的问题,您可以随意停在这里,知道代码简单而有效。
当然......这可以简化。最后一个if
语句可以更改为return
:
if (a) return true; return false;
转换为:
return a;
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
if (!dotted)
return true;
if (all)
return true;
if (implied && almost)
return true;
return directory && isDot;
}());
当然可以通过将最后if
再次转换为return
来简化:
if (a) return true; return b;
转换为:
return a || b;
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
if (!dotted)
return true;
if (all)
return true;
return (implied && almost) ||
(directory && isDot);
}());
......再次:
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
if (!dotted)
return true;
return (all) ||
(implied && almost) ||
(directory && isDot);
}());
......再次:
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
if (directory && !isDot)
return false;
return (!dotted) ||
(all) ||
(implied && almost) ||
(directory && isDot);
}());
......再次:
if (a) return false; return b;
转换为:
return !a && b;
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
return !(directory && !isDot) && (
(!dotted) ||
(all) ||
(implied && almost) ||
(directory && isDot)
);
}());
可以进一步简化此操作
!(a && b)
转换为:
!a || !b
include = (function () {
//set up some simple names for concepts:
var directory = options.directory;
var isDot = file === '.';
var all = options.all;
var almost = options.almostall;
return (!directory || isDot) && (
(!dotted) ||
(all) ||
(implied && almost) ||
(directory && isDot)
);
}());
你有它,就像逻辑一样简单。当然,您可以选择将变量扩展回原来的定义,但我鼓励您不要这样做。我实际上鼓励你不要简化if..return
语句的简单链。
如果您使代码更加简洁,那么阅读和理解更具挑战性,这使得调试更具挑战性。我很可能在这篇文章的某个地方犯了一个错误,同时简化了#34;代码,如果出现错误,在阅读&&
和||
系列运算符时,并不是很明显。