Skip to main content
 首页 » 编程设计

coffeescript 中类似 python 的字符串插值

2025年05月04日12sky-heaven

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

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

请您参考如下方法:

您可以将所有这些条目设为一个函数,然后使用带参数的字典对它们应用:

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