CoffeeScript:由空分隔符连接的理解中的虚假逗号
CoffeeScript: Spurious comma in comprehension joined by empty separator
我想要一个方便的函数来连接 jQuery parent > child 选择器字符串。我无法在 CS 1.10.0 中使用以下内容(也在 1.7.1 中测试过)。我做错了什么?
pcsel = (parent_sel, child_sels...) ->
### Uitlity for forming parent > child selector string ###
childchain = [" > " + child for child in child_sels]
parent_sel + childchain.join('')
console.log pcsel("foo", "bar") # OK. yields "foo > bar"
console.log pcsel("foo", "bar", "glop") # BAD. yields "foo > bar, > glop"
# Sanity check
console.log "foo" + [" > bat", " > glop"].join('') # OK. yields "foo > bar > glop"
谢谢!
(我也将此作为问题发布在 CS 存储库中)
循环理解:
expr for e in array
计算为一个数组。这意味着:
[ expr for e in array ]
实际上是一个单元素数组,其第一个(也是唯一一个)元素是循环中的数组。更明确地说:
i for i in [1,2,3]
是[1,2,3]
但是这个:
[ i for i in [1,2,3] ]
是[[1,2,3]]
。
您的问题是 pcsel
中的 childchain
以额外的嵌套级别结束,并且 join
调用的字符串化添加了意外的逗号。
解决方法是修复pcsel
:
childchain = (" > " + child for child in child_sels)
# -----------^-------------------------------------^
您需要圆括号(而不是方括号)来解决优先级问题;圆括号 (()
) 和方括号 ([]
) 的功能完全不同,因此您需要使用正确的圆括号。
据我所知,您看到的行为是符合预期的。如果您将 splat 替换为显式数组,则代码的行为如下:
coffee> ["> " + ['bar']] # => ['> bar']
coffee> ["> " + ['bar', 'baz']] # =>['> bar,baz']
您还会在节点中看到相同的行为:
> [">" + ['bar']] // => ['>bar']
> ["> " + ['bar', 'baz']] // => ['> bar,baz']
您可以在多次调用 .join
后实现您的目标,或者通过执行以下操作:
pcsel = (parent_sel, child_sels...) ->
child_sels.reduce (memo, sel) ->
memo + " > #{sel}"
, parent_sel
console.log pcsel("foo", "bar") # => foo > bar
console.log pcsel("foo", "bar", "glop") # => foo > bar > glop
console.log pcsel("foo", "bar", "glop", "baz") # => foo > bar > glop > baz
我想要一个方便的函数来连接 jQuery parent > child 选择器字符串。我无法在 CS 1.10.0 中使用以下内容(也在 1.7.1 中测试过)。我做错了什么?
pcsel = (parent_sel, child_sels...) ->
### Uitlity for forming parent > child selector string ###
childchain = [" > " + child for child in child_sels]
parent_sel + childchain.join('')
console.log pcsel("foo", "bar") # OK. yields "foo > bar"
console.log pcsel("foo", "bar", "glop") # BAD. yields "foo > bar, > glop"
# Sanity check
console.log "foo" + [" > bat", " > glop"].join('') # OK. yields "foo > bar > glop"
谢谢!
(我也将此作为问题发布在 CS 存储库中)
循环理解:
expr for e in array
计算为一个数组。这意味着:
[ expr for e in array ]
实际上是一个单元素数组,其第一个(也是唯一一个)元素是循环中的数组。更明确地说:
i for i in [1,2,3]
是[1,2,3]
但是这个:
[ i for i in [1,2,3] ]
是[[1,2,3]]
。
您的问题是 pcsel
中的 childchain
以额外的嵌套级别结束,并且 join
调用的字符串化添加了意外的逗号。
解决方法是修复pcsel
:
childchain = (" > " + child for child in child_sels)
# -----------^-------------------------------------^
您需要圆括号(而不是方括号)来解决优先级问题;圆括号 (()
) 和方括号 ([]
) 的功能完全不同,因此您需要使用正确的圆括号。
据我所知,您看到的行为是符合预期的。如果您将 splat 替换为显式数组,则代码的行为如下:
coffee> ["> " + ['bar']] # => ['> bar']
coffee> ["> " + ['bar', 'baz']] # =>['> bar,baz']
您还会在节点中看到相同的行为:
> [">" + ['bar']] // => ['>bar']
> ["> " + ['bar', 'baz']] // => ['> bar,baz']
您可以在多次调用 .join
后实现您的目标,或者通过执行以下操作:
pcsel = (parent_sel, child_sels...) ->
child_sels.reduce (memo, sel) ->
memo + " > #{sel}"
, parent_sel
console.log pcsel("foo", "bar") # => foo > bar
console.log pcsel("foo", "bar", "glop") # => foo > bar > glop
console.log pcsel("foo", "bar", "glop", "baz") # => foo > bar > glop > baz