Coffeescript:在括号之前用空格调用函数 [英] Coffeescript: invoking function with space before parens

查看:220
本文介绍了Coffeescript:在括号之前用空格调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用一个在括号之前有一个空格的函数时,它会给出一个错误,表示未检测到,

When I invoke a function with a space before the parens, it gives an error saying unepected ,

sum = (a, b) ->
  a+b
console.log (sum (1, 2))

错误:意外,

console.log(sum(1,2))

error: unexpected ,
console.log (sum (1, 2))

它指向1和2之间的逗号

it points to the comma between 1 and 2

为什么是奇怪的行为?

推荐答案

两种方式:

foo(bar) # with parens
foo bar  # without parens

由于 sum 1,2),那么你正在进行 sum 传递(1,2)作为第一个参数,相当于:

Since you have a space between sum and (1, 2), then you are making a unparenthesized functional call of sum passing (1, 2) as the first parameter, equivalent to this:

bar = (1, 2)
sum bar

问题是(1,2)不是有效的CoffeeScript表达式。要传递两个参数,您必须使用以下两个参数之一:

The problem is that (1, 2) is not a valid CoffeeScript expression. To pass two parameters, you have to use either of:

sum(1, 2)
sum 1, 2

这篇关于Coffeescript:在括号之前用空格调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆