如何在函数调用期间实现可选括号?(函数重载) [英] How to implement optional parentheses during function call? (function overloading)

查看:67
本文介绍了如何在函数调用期间实现可选括号?(函数重载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我猜这是不可能的,但我希望 ff() 做同样的事情.

My guess is that this is not possible, but I'd like f and f() to do the same thing.

   var f = function(str){ console.log(str||'foo'); }();

   f;                      // wanted output: 'foo'
   f();                    // wanted output: 'foo'
   f('bar');               // wanted output: 'bar'

因为 f 不再是函数定义,所以 f() 似乎不可能,但也许我遗漏了一些东西.有什么建议吗?

Because f is no longer a function definition, it doesn't seem possible to do f(), but maybe I'm missing something. Any suggestions?

推荐答案

不,那是不可能的.需要括号来确定应该调用的函数.

No that is not possible. The parentheses are required to determine that the function should be called.

表达式f()的值是调用函数的结果,而f的值是函数本身(和f.tostring() 如果你显示它.

The value of the expression f() is the result of calling the function, while the value of f is the function itself (and f.tostring() if you display it).

这篇关于如何在函数调用期间实现可选括号?(函数重载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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