Function and Object Javascript
Solution 1:
'Function' function is an Object, then 'Function' object needs another constructor function to build it
No. Function
is a native, builtin object whose properties and behavior are defined in section 15.3 of the EcmaScript specification. It was not built by an js function.
Think of it like that: There is a function somewhere in the code of your EcmaScript environment that builds function objects - it is called whenever your script encounters a function expression or declaration. The global Function
function is a wrapper for that function to make it accessible for scripts. All function objects which that function returns inherit from the Function.prototype
object - it looks like they were constructed by Function
. Also the Function.prototype.constructor
property that all functions inherit is defined to point to Function
.
Solution 2:
A function in JS embeds 2 concepts:
- An entity
- A functionality
A function entity is some sort of "capsule" that contains a functionality, i.e., the power of transforming several inputs into an output. This capsule is what we know as "object". At the end of this recursion you find the identity Function.constructor === Function
, which sets the limits of the introspective features of the language. The rest of JS functionalities cannot be accessible by the language itself because there not exists any capsule or object that embeds them.
In JS you cannot define standalone 'functionalities' but you create objects that implement such functionalities that can be treated as any other objects. The Function
object is the core object for implementing functionalitites. Either if you define named or anonymous functions (by means of the function
keyword) you are creating a Function object that is bound either to a name (for named functions) or directly to a variable (unnamed functions).
function foo(a, b) { return a+b; } //This function is a Function object bound to the name `foo`
var a = function(a, b) { return a+b; } //the Function object is bound to `a`
In the same manner the Array object has the []
operator, which is used to access to array elements, you may interpret ()
as an operator of the Function object which is used for invoking its embedded functionality.