函数对象和可调用对象有什么区别? [英] What is the difference between a function object and a callable object?

查看:147
本文介绍了函数对象和可调用对象有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看到演示文稿ECMAScript 5中的变化。并且有这个幻灯片声明:
$ b


功能 vs Callable

  typeof f ==='function'//→f是Callable 
({})。toString.call(f)==='[object函数]'//→f是函数


任何人都可以解释给我 Callable 之间的区别是什么?

一般来说,一个对象可以被调用而不是一个函数。在一切都是对象(包括函数)的语言中,可调用对象不必从Function类下降。



在JS中,它看起来像一个Callable任何具有[[Call]]内部方法的内容(由'function'类型标识,而不是'object')。函数(如在幻灯片中使用的)是Function对象的后代。我可能是错的,但在脚本中,只能创建函数,而ECMAScript实现可以定义不是函数的Callables。



如果您尝试使用用匿名函数/函数表达式和声明函数滑动,结果是一样的。

// =='function'
({})。toString.call(function(){})// =='[object Function]'
function foo(){}
typeof foo; // =='function'
({})。toString.call(foo)// =='[object Function]'


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?

解决方案

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.

typeof function() {}; // == 'function'
({}).toString.call(function() {}) // == '[object Function]'
function foo() {}
typeof foo; // == 'function'
({}).toString.call(foo) // == '[object Function]'

这篇关于函数对象和可调用对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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