对象字面量中可使用中括号做为属性,表示属性也能是一个变量

  • 在对象字面量中可使用中括号做为属性,表示属性也能是一个变量了。 
  1. const name = 'Jane';
  2.  
    const age = 20
  3.  
     
  4.  
    const person = {
  5.  
    [ name]: true,
  6.  
    [ age]: true
  7.  
    }

在ant-design的源码实现中,就大量使用了这种方式来拼接当前元素的className,例如:javascript

  1.  
    let alertCls = classNames(prefixCls, {
  2.  
    [`${prefixCls}-${type}`]: true,
  3.  
    [`${prefixCls}-close`]: !this.state.closing,
  4.  
    [`${prefixCls}-with-description`]: !!description,
  5.  
    [`${prefixCls}-no-icon`]: !showIcon,
  6.  
    [`${prefixCls}-banner`]: !!banner,
相关文章
相关标签/搜索