【问题标题】:python-like interpolation of string in coffeescript在coffeescript中类似python的字符串插值
【发布时间】:2015-08-08 03:19:37
【问题描述】:

我有这样的命名空间有错误

errorMessages=
  error404Find: "Hosts not found"
  error404FindByHost: "Host - #{hostName} - not found"
  error400: "No host"

hostName 应分配给 null 并在如下函数中覆盖:

this.hostName = 'smth'

或者我可以在 python 中使用类似的东西:

errorMessages=
  {'error404Find': "Hosts not found"
  'error404FindByHost': "Host - {hostName} - not found"
  'error400': "No host"}
errorMessages['error404FindByHost'].format(hostName='smth')

【问题讨论】:

    标签: python string coffeescript string-interpolation language-comparisons


    【解决方案1】:

    您可以将所有这些条目设为函数,然后通过参数将字典应用于它们:

    errorMessages=
      error404Find:-> "Hosts not found"
      error404FindByHost:-> "Host - #{@hostName} - not found"
      error400:-> "No host"
    
    alert errorMessages["error404FindByHost"].apply hostName:"host1"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 2015-08-02
      • 1970-01-01
      • 2012-08-01
      • 2011-11-15
      • 2012-08-18
      • 1970-01-01
      • 2013-06-12
      相关资源
      最近更新 更多