Skip to content Skip to sidebar Skip to footer

What Is The Difference Between A Function Object And A Callable Object?

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' //

Solution 1:

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.

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

Post a Comment for "What Is The Difference Between A Function Object And A Callable Object?"