【问题标题】:Any idea why this happens in coffeescript知道为什么会在咖啡脚本中发生这种情况
【发布时间】:2015-07-08 02:01:12
【问题描述】:

在我运行这些行之后,我在咖啡脚本中有以下代码,str 的值仍然是“d41d8cd98f00b204”。有什么想法我可能做错了吗?

dataDict = {email: "johndoe@gmail.com", t:213213.213213}
apiFields = ['email', 'password', 'backup_email', 'firstname', 
             'lastname', 'dob', 'username', 'position', 'industry', 
             'institution', 'verificationcode', 'confirmcode', 
             'signuphost', 'responses', 't']

str = "d41d8cd98f00b204"
for ind in apiFields
  str = str + dataDict[ind] if ind in dataDict
console.log(str)

【问题讨论】:

    标签: javascript coffeescript


    【解决方案1】:

    我愿意:

    append = dataDict[ind]
    str = str + append if append
    

    你所做的编译成:

    if (__indexOf.call(dataDict, ind) >= 0) str = str + dataDict[ind];
    

    在哪里

    __indexOf === [].indexOf //Array.prototype's indexOf
    

    Array.prototypeindexOf 不适用于非数组对象。

    【讨论】:

      【解决方案2】:

      我认为in 仅适用于数组,请尝试:

      str = str + dataDict[ind] if dataDict[ind]
      

      【讨论】:

        【解决方案3】:

        来自fine manual

        您可以使用in 测试数组是否存在,使用of 测试JavaScript 对象键是否存在。

        in 用于检查元素是否在数组中(就像您使用 for ... in 遍历数组一样),如果您想测试键是否在对象中,您将使用 of (就像你使用for ... of 来迭代一个对象):

        str = str + dataDict[ind] if ind of dataDict
        # -------------------------------^^
        

        【讨论】:

          【解决方案4】:

          检查if ind in dataDict的扩展:

          if (indexOf.call(dataDict, ind) >= 0) {
            str = str + dataDict[ind];
          }
          

          检查 if dataDict.hasOwnProperty(ind) 应该可以正常工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-02
            • 2018-12-19
            • 2021-01-24
            • 1970-01-01
            • 2013-06-09
            • 2021-04-30
            • 2010-09-22
            相关资源
            最近更新 更多