知道为什么在 coffeescript 中会发生这种情况
Any idea why this happens in coffeescript
在我 运行 这些行之后,我在 coffeescript 中有以下代码,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)
检查if ind in dataDict
的扩展:
if (indexOf.call(dataDict, ind) >= 0) {
str = str + dataDict[ind];
}
检查 if dataDict.hasOwnProperty(ind)
应该可以正常工作。
我认为in
只适用于数组,试试:
str = str + dataDict[ind] if dataDict[ind]
我愿意:
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.prototype
的 indexOf
不适用于非数组对象。
来自fine manual:
You can use in
to test for array presence, and of
to test for JavaScript object-key presence.
in
用于检查一个元素是否在数组中(就像你使用 for ... in
遍历数组一样),如果你想测试一个键是否在一个对象中你将使用 of
(就像您使用 for ... of
遍历对象一样):
str = str + dataDict[ind] if ind of dataDict
# -------------------------------^^
在我 运行 这些行之后,我在 coffeescript 中有以下代码,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)
检查if ind in dataDict
的扩展:
if (indexOf.call(dataDict, ind) >= 0) {
str = str + dataDict[ind];
}
检查 if dataDict.hasOwnProperty(ind)
应该可以正常工作。
我认为in
只适用于数组,试试:
str = str + dataDict[ind] if dataDict[ind]
我愿意:
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.prototype
的 indexOf
不适用于非数组对象。
来自fine manual:
You can use
in
to test for array presence, andof
to test for JavaScript object-key presence.
in
用于检查一个元素是否在数组中(就像你使用 for ... in
遍历数组一样),如果你想测试一个键是否在一个对象中你将使用 of
(就像您使用 for ... of
遍历对象一样):
str = str + dataDict[ind] if ind of dataDict
# -------------------------------^^