Javascript Scala函数何时执行

Javascript Scala函数何时执行,javascript,function,scala,Javascript,Function,Scala,在Javascript中,我可以这样描述一个函数 function showString(){ console.log("this is a string") }; 这样,在控制台中,函数和已执行函数之间存在严格的差异 > function showString(){ console.log("this is a string") }; > showString function showString(){ console.log("this is a string") } &

在Javascript中,我可以这样描述一个函数

function showString(){ console.log("this is a string") }; 
这样,在控制台中,函数和已执行函数之间存在严格的差异

> function showString(){ console.log("this is a string") }; 
> showString
function showString(){ console.log("this is a string") }
> showString()
this is a string 
在斯卡拉,我现在也在做同样的事情

def showname() = println("this is a string")
但是,当我在控制台中运行时,它似乎总是执行函数,而不是仅仅传递函数:

scala> def showname() = println("this is a string")
showname: ()Unit
scala> showname // I am expecting a function, not an executed function
this is a string
scala> showname()
this is a string // I am expecting an executed function

Scala处理函数是否不同?我的期望错了吗

showname
实际上是一个方法,而不是一个函数,如果要获取函数,可以使用下划线语法:

scala> def showname() = println("this is a string")
showname: ()Unit

scala> showname
this is a string

scala> showname _
res1: () => Unit = <function0> 
如果修改其签名并尝试在不使用参数的情况下调用它,还可以检查
showname
是否为方法:

scala> def showname(s: String) = println("this is a string")
showname: (s: String)Unit

scala> showname
<console>:9: error: missing arguments for method showname;
follow this method with `_' if you want to treat it as a partially applied function
              showname
scala>def showname(s:String)=println(“这是一个字符串”)
showname:(s:String)单位
scala>showname
:9:错误:缺少方法showname的参数;
如果要将其视为部分应用的函数,请使用“\u1”遵循此方法
秀名

函数和方法之间的区别在于。

showname
实际上是一个方法,而不是函数,如果要获取函数,可以使用下划线语法:

scala> def showname() = println("this is a string")
showname: ()Unit

scala> showname
this is a string

scala> showname _
res1: () => Unit = <function0> 
如果修改其签名并尝试在不使用参数的情况下调用它,还可以检查
showname
是否为方法:

scala> def showname(s: String) = println("this is a string")
showname: (s: String)Unit

scala> showname
<console>:9: error: missing arguments for method showname;
follow this method with `_' if you want to treat it as a partially applied function
              showname
scala>def showname(s:String)=println(“这是一个字符串”)
showname:(s:String)单位
scala>showname
:9:错误:缺少方法showname的参数;
如果要将其视为部分应用的函数,请使用“\u1”遵循此方法
秀名

函数和方法之间的区别在于。

这不是函数,而是方法。这是一个函数:

val showname = () => println("this is a string")

showname
// => res0: () => Unit = <function0>

showname()
// this is a string
val showname=()=>println(“这是一个字符串”)
秀名
//=>res0:()=>Unit=
showname()
//这是一根绳子

正如您所看到的,函数的行为与您期望的函数一样。

这不是函数,而是方法。这是一个函数:

val showname = () => println("this is a string")

showname
// => res0: () => Unit = <function0>

showname()
// this is a string
val showname=()=>println(“这是一个字符串”)
秀名
//=>res0:()=>Unit=
showname()
//这是一根绳子

正如你所看到的,函数的行为就像你从函数中所期望的那样。

我认为函数对象是应用程序之后得到的东西的更好的名称(虽然,并不理想,因为scala中的对象还有另一个含义——singleton)。@om nom nom我明白你的意思,但老实说,我觉得很困惑,而且我从来没有读过这种命名(或者可能我没有注意到),我总是坚持功能。好吧:-)我只提出了替代方案。我从来没有很好地阅读过这种命名。而且我也读过很多次这个问题,我可能从来没有真正注意过命名,谢谢你的注意。我认为函数对象是一个更好的名称,适合你在应用程序之后得到的东西(虽然,不理想,因为scala中的对象还有另一个含义——singleton)@om-nom-nom我明白你的意思,但老实说,我觉得很困惑,而且我从来没有读过这种命名(或者可能我没有注意到),我总是坚持函数。好吧:-)我只提出了替代方案。我从来没有读过这样的命名,而且我也读过很多次这个问题,我可能从来没有真正注意过命名,谢谢你的注意。