2

When you do a "typeof" on both of these types you get "function" for functions and "object" for objects, but isn't it so that functions are special objects, if so what are the properties that differentiate a function from an object?

2 Answers 2

7

When you do a "typeof" on both of these types you get "function" for functions and "object" for objects,

First of all, the specification for typeof is basically just a lookup table, where it says "if the value is a function object, return the string "function"). So it doesn't provide the real data type of the value (which would be object for functions).

but isn't it so that functions are special objects

Yes. Functions are so called callable objects.

Objects have, in addition to "normal" properties, so called "internal" properties. You can think of these as some kind of internal state, that needs to be maintained for the object to work correctly, but that is not accessible in user code.

Some of these internal properties make certain objects special because not every object has them. One of them is [[Call]] (internal properties are denoted with double brackets), which contains code in some implementation-specific format.

When you call a value (i.e. foo()) the interpreter first checks whether the value is an object and then checks whether it has an intern [[Call]] property. If yes, then the code stored in that property is executed.

Here is a very rough example of how this could look like internally:

// Simulates a function call implementation, i.e. what happens when
// you do `foo()`
function call(value) {
  if (typeof value !== "object") {
    throw new Error('Not an object');
  }
  if (!value["[[Call]]"]) {
     throw new Error('Not a function');
  }
  return eval(value["[[Call]]"]);
}

// Simulated function object that has a name ("normal" property)
// and the internal property [[Call]].
// This would be the internal representation for something like
// function func() {
//   console.log('some code');
// }
var func = {
  name: "func",
  "[[Call]]": "console.log('I\\'m a function!');",
};

call(func);

Side note: If you know Python, then this concept should be familiar to you, because Python allows you to make arbitrary objects callable by implementing __call__.

In addition to [[Call]] there is also [[Construct]]. We actually distinguish between callable and constructable functions. Constructable functions are those that can be invoked with new. Functions created via function ... are both callable and constructable. Arrow functions are only callable, functions created via class ... are only constructable. And this distinction is made based on whether [[Call]] or [[Construct]] or both are set.

if so what are the properties that differentiate a function from an object

In addition to these special internal properties, function objects also have all properties defined on Function.prototype:

console.dir(Function.prototype);
// etc

(and Function.prototype "extends" Object.prototype) which is where .call and .apply are defined, but these alone do not make functions special. The internal [[Call]] property is what makes them special.


The same applies to other built-in "classes" such as Array, Date or RegExp. All instances of these classes have additional methods/properties that are defined on Array.prototype, Date.prototype, etc.

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

Comments

0

Functions (which are first-class objects) can be called, while other objects cannot.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions for more detail.

2 Comments

if so "Pal Kerecsenyi" can I make this statement in JS that says "Every function is an object but no object is a function".
Every function is an object, but the second part of the statement isn't quite true. A function is an object of type Function.

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.