什么是 javascript const 类?

Ego*_*_sx 0 javascript reactjs redux

我正在从http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html学习 Redux&React 。

在代码代码片段中:

import React from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
import {connect} from 'react-redux';
import Winner from './Winner';
import Vote from './Vote';

export const Voting = React.createClass({
  mixins: [PureRenderMixin],
  render: function() {
    return <div>
      {this.props.winner ?
        <Winner ref="winner" winner={this.props.winner} /> :
        <Vote {...this.props} />}
    </div>;
  }
});

function mapStateToProps(state) {
  return {
    pair: state.getIn(['vote', 'pair']),
    winner: state.get('winner')
  };
}

export const VotingContainer = connect(mapStateToProps)(Voting);
Run Code Online (Sandbox Code Playgroud)

作者正在从“纯”组件创建“有线”反应组件。我对代码中显示的两个“const”关键字有点困惑。我可以理解 javascript 中的 const 值和对象,但从 OO 的角度来看,const 类对我来说没有意义。

如果我从第一种和/或第二种情况中删除“const”关键字会有什么不同吗?

Geo*_*lah 5

Const 是一个块范围的赋值,它分配一个常量引用(不是常量值)。这意味着你不能稍后在该模块中意外地重新分配 Voting 或 VotingContainer。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

(是的,您可以使用 let/var 切换 const)

  • 此示例假设您使用的是 ES6 转译器。它有 ES6 模块。Babel 会将 `const` 变成 `var`。 (2认同)