CoffeeScript function call - parameters and brackets
Exists such a Javascript code:
var re = /some_regex/g;
return re.exec(link.attr('href'))[0]
How to call this in CoffeeScript In CoffeeScript there is no need in brackets for parameters but there is another call of function in param.
I've tried:
re = /some_regex/g
re.exec link.attr 'href' [0] # compile error: unexpected [
re.exec (link.attr 'href')[0] # javascript: re.exec((link.attr('href'))[0]);
re.exec (link.attr('href'))[0] # j开发者_高级运维avascript: re.exec((link.attr('href'))[0]);
how to do this? or I should make
// adding new variable
temp = re.exec link.attr 'href'
temp[0]
The space after re.exec
is causing a problem, because it causes the CoffeeScript compiler to think that (link.attr('href'))[0]
is the argument.
The correct way to do this is to do it exactly like in JavaScript, with no space:
re.exec(link.attr('href'))[0]
If you really badly want to use the no-parens syntax on this line, this would also work:
re.exec(link.attr 'href')[0]
(They compile to the same result)
Wouldn't this be clearer if you simply parenthesized the part that absolutely needs it, and do the rest in the most CoffeeScript-like-way?
(re.exec link.attr 'href')[0]
or maybe even (if 'href' is just another attribute):
(re.exec link.attr.href)[0]
or, better, yet, to be more clear, along the lines of what you suggested originally:
matches = re.exec link.attr.href
matches[0] // idiomatic to re.exec: first element is matched RE
精彩评论