我一直在看这段代码。 https://www.learnrxjs.io/recipes/alphabet-invasion-game.html
const game$ = combineLatest(keys$, letters$).pipe(
scan < [string, Letters],
State >
((state, [key, letters]) => (
letters.ltrs[letters.ltrs.length - 1] &&
letters.ltrs[letters.ltrs.length - 1].letter === key
? ((state.score = state.score + 1), letters.ltrs.pop())
: noop,
state.score > 0 && state.score % levelChangeThreshold === 0
? ((letters.ltrs = []),
(state.level = state.level + 1),
(state.score = state.score + 1),
intervalSubject.next(letters.intrvl - speedAdjust))
: noop,
{ score: state.score, letters: letters.ltrs, level: state.level }
),
{ score: 0, letters: [], level: 1 }),
takeWhile(state => state.letters.length < endThreshold)
);
这是我看到的另一个使用很多逗号的代码
const game$ = combineLatest(state$, player$)
.pipe(
scan<[State, [number[][], Key]], [State, [number[][], Key]]>(
([state, [brick, key]]) => (
handleKeyPress(state, brick, key),
brick = rotate(state, brick, key),
brick = collide(state, brick),
score(state),
resetKey(key),
[state, [brick, key]]
)),
tap(([state, [brick, key]]) => render(state, brick)),
takeWhile(([state, [brick, key]]) => !state.game[1].some(c => c === brck)),
finalize(renderGameOver)
);
我只是不明白为什么扫描功能体内有这么多的昏迷。 noop,
这里有两个。在我的另一个示例中,每行都有一个逗号。
此外,我不明白为什么我们要在此处传递[键,字母]作为数组。 (状态,[键,字母])
我浏览了以前提出的旧扫描问题,但是没有一个解决这个逗号的问题。
答案 0 :(得分:2)
这只是花哨/巧妙/过于复杂的javascript语法/功能用法。首先,所有这些逗号:有一个逗号运算符 ,它计算每个操作数(从左到右)并返回最后一个操作数的值。 (文档:coma operator)。
简单的例子:
c = (
a = 1, // assigns value 1 to variable a
b = 2, // assigns value 2 to variable b
console.log(a, b), // log values to console
"c"
)
将输出:
1 2 // console log
"c" // value of last operand
...和c
变量将具有"c"
值:
console.log(c) // prints "c"
另一个是另一个js功能-destructuring assignment
const myFn = ([a, [b, c]]) => console.log(a, b, c);
const arrayOfThatShape = [1, [2, 3]];
myFn(arrayOfThatShape) // prints 1 2 3
因此,基本上所有这些与RxJS都不相关,也不与函数式反应性编程相关。 IMO这样的示例过于复杂,给RxJS新手带来了不必要的噪音。