我试图理解this Tic-Tac-Toe (30 lines of code) game的代码并遇到了这个奇怪的j符号。
t[id] ? ai() : move(id, 'ai');
!checkEnd() ? (role == 'player') ? ai() : null : reset()
我知道这是if语句的缩短版本,但不知道如何转换它。
提前致谢。
答案 0 :(得分:2)
这是javascript中的ternary operator。
t[id] ? ai() : move(id, 'ai');
转换为:
if (t[id])
ai();
else
move(id, 'ai');
!checkEnd() ? (role == 'player') ? ai() : null : reset()
来:
if (!checkEnd())
if (role == 'player')
ai();
else
;
else
reset();
答案 1 :(得分:0)