解释这个JS的简写:`var myVar = i === 0`

时间:2018-03-18 08:21:04

标签: javascript reactjs syntax

我正在学习与http://buildwithreact.com/tutorial/state

的反应

有一段代码片段,其中包含我之前从未遇到的一段JS代码 - 第5行(包含注释)。我认为这是一种速记。它表达了什么?

var BoardSwitcher = React.createClass({
  render: function() {
    var boards = [];
    for (var ii = 0; ii < this.props.numBoards; ii++) {
      var isSelected = ii === 0;  // WHAT DOES THIS MEAN ???
      boards.push(
        <Board index={ii} selected={isSelected} key={ii} />
      );
    }

    return (
      <div>
        <div className="boards">{boards}</div>
        <button>Toggle</button>
      </div>
    );
  }
});

1 个答案:

答案 0 :(得分:0)

=== is an operator that tests equality without autocasting,并返回一个布尔值; true如果两个值相等且类型相同,则false。然后将此布尔值分配给变量isSelected

You can interactively try it看看会发生什么:

> var ii = 0;
> var isSelected = ii === 0;
> console.log(isSelected);
true
> var ii = 1;
> var isSelected = ii === 0;
> console.log(isSelected);
false