我遇到过这段代码
render: function() {
var boards = [];
for (var ii = 0; ii < this.props.numBoards; ii++) {
// We can compare to state here so we're no longer always selecting the first board.
var isSelected = ii === this.state.selectedIndex;
boards.push(
<Board index={ii} selected={isSelected} />
);
}
行isSelected = ii === this.state.selectedIndex
是如何执行的?
答案 0 :(得分:4)
以下代码分两步执行
isSelected = ii === this.state.selectedIndex
1. ii === this.state.selectedIndex // comparator operator
2. isSelected = (result of step 1) // assignment operator
答案 1 :(得分:4)
为清楚起见,该行应写为:
isSelected = (ii === this.state.selectedIndex);
这是因为==
和===
用于比较,语句ii === this.state.selectedIndex
将返回true
或false
,因此isSelected
将是true
或false
。就像if (ii === this.state.selectedIndex)
只会在比较语句为真时执行其代码块一样。