什么是javascript const类

what is javascript const class?

本文关键字:const javascript 什么      更新时间:2023-09-26

我正在学习Redux&反应来自http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html.

在一个代码片段中:

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);

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

如果我从第一个和/或第二个案例中删除"const"关键字,会有什么不同吗?

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

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

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