3

I've been reading the book named "Object Oriented Javascript" by Stoyan Stefanov. I see this sentence:

function is actually an object that is built with 'Function' constructor function (with capital F).

The author demonstrates this with some nice examples. However, based on that statement, I got this question that can't be answer by myself. As 'Function' constructor is a function, so 'Function' function is an Object, then 'Function' object needs another constructor function to build it and that another constructor function is again an Object (because it's a function).

Well, I end up with this endless logic. Can someone help me point out the wrong point in my thinking?

1
  • 7
    Function.constructor === Function - but native objects are provided through the environment, not through scripting, so that's why this is possible. Commented Oct 14, 2012 at 11:01

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

A function in JS embeds 2 concepts:

  1. An entity
  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.