函数对象和可调用对象有什么区别?
我最近看到了有关更改的演示文稿ECMAScript 5。 还有一张幻灯片陈述:
函数与可调用
typeof f === 'function' // → f 是可调用的 ({}).toString.call(f) === '[object Function]' // → f 是一个函数
谁能向我解释一下 Function 和 Callable 之间的区别是什么?
I recently saw the presentation about the changes in ECMAScript 5. And there was a slide with this statement:
Function vs Callable
typeof f === 'function' // → f is Callable ({}).toString.call(f) === '[object Function]' // → f is a Function
Can anyone explain to me what the difference between Function and Callable is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,对象可以在不是函数的情况下被调用。 在一切都是对象(包括函数)的语言中,可调用对象不必从 Function 类继承。
在 JS 中,Callable 看起来像是具有内部 [[Call]] 方法的任何东西(由“函数”类型标识,而不是“对象”)。 Function(如幻灯片中所使用的)是 Function 对象的后代。 我可能是错的,但在脚本中你只能创建函数,而 ECMAScript 实现可以定义不是函数的可调用对象。
如果您使用匿名函数/函数表达式和声明函数尝试幻灯片中的代码片段,结果是相同的。
Generally speaking, an object can be callable without being a function. In a language where everything is an object (including functions), callable objects don't have to descend from a Function class.
In JS, it looks like a Callable is anything that has the internal [[Call]] method (identified by a typeof of 'function', as opposed to 'object'). A Function (as used in the slide) is a descendant of the Function object. I could be wrong, but within a script you can only create Functions while the ECMAScript implementation can define Callables that aren't Functions.
If you try the code fragment from the slide with both anonymous functions/function expressions and with declared functions, the results are the same.