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

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

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

  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,
  7.  

猜你喜欢

转载自www.cnblogs.com/daiwenru/p/9388875.html