views:

107

answers:

4

I am a self taught web developer and am still trying to come to grips with some JavaScript fundamentals. Below are some quotes extracted from Douglas Crockford's Good Parts.

"Functions in JavaScript are Objects"

"In JavaScript, arrays are objects, functions are objects, regular expressions are objects, and, of course, objects are objects"

"Every object is linked to a prototype object from which it can inherit properties" (namely constructor, toString, ...)

If Function is an Object then why

 console.log(typeof Function);  // function 

is its type a function and not object

 console.log(Object.constructor);  // Function()

is it the constructor of its 'parent'

 console.log(Function.constructor);  // Function()

puzzled so the constructor is in-fact a function?

 console.log(typeof Function.prototype); // Function

is the type of its prototype a function and not an object? i thought it inherited from Object

Answers to these questions will greatly assist my understanding on JavaScript.

+2  A: 

When we say, "a function is an object", we don't mean "is" as in "instead of", we mean it in the same sense as "a cat is an animal". If someone asked you what kind of pet you had, you wouldn't answer "an animal". typeof would be useless if it always responded object.

A function is an object, but that's not an interesting thing for typeof to return, since it's a static quality of the language itself, not something that needs to be reported at runtime.

Ned Batchelder
+7  A: 

If Function is an Object then why is its type a function and not object?

Because the typeof operator is defined like that, probably for usability:

  • Object (native and does not implement [[Call]]) returns "object"
  • Object (native or host and does implement [[Call]]) returns "function"
  • Object (host and does not implement [[Call]]) returns an Implementation-defined value that may not be "undefined", "boolean", "number", or "string".

[[Call]] is an internal property of an object that identifies the object as a function (callable). A non-native object is an object provided by the host (e.g. browser), such as a DOM object or an instance of ActiveXObject.

puzzled so the constructor is in-fact a function?

Why wouldn't it be? Constructors are functions. Instances can only be constructed using functions. Object.constructor is a function, but it's also an object. See the following:

console.log((function () { }) instanceof Object);
//-> true

Also, from the ECMAScript speficiation:

Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype (15.3.4), as the value of its [[Prototype]] internal property.

Unless otherwise specified every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.2.4), as the value of its [[Prototype]] internal property, except the Object prototype object itself.

And also, to answer your final puzzlement:

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

Andy E
+1 Thanks for this explanation it really makes things more clear.
Q_the_dreadlocked_ninja
+1  A: 

The typeof operator would be quite useless if it always returned "object", wouldn't it? Everything is an object, but it can be other things too. For example, a string is an object, but it is also a string :) The operator returns the name of the most specific type so to speak, not the most generic one. That should explain why typeof Functionis "function".

As for the constructor property, the constructor is a function that is invoked by the operator new when an object is created. It is always a function, regardless of whether the object itself is Object, Functionor something else.

Jakob
Primitives - strings, numbers and booleans - aren't objects, but are implicitly converted to objects when you try and access a property or method of their object counterpart on them. For instance, `"jim" instanceof Object` will return false, but `new String("jim") instanceof Object` will return true.
Andy E
A: 

The console.log(typeof Function); shows that the object is of type Function and not object.

To give you an example:

function squareValue(var x) {
    return x * x;
}

can be loosely translated as

var squareValue = new Function("x", "return x*x");

and thus, doing

var valueSquared = squareValue(x);

in any of these 2 examples, will produce the same results...

Hence why every function in Javascript is a Function object.

The .constructor, .prototype, .toString on javascript Object/Function are all functions in their respective objects hence why you have the output as "function".

This is based according to the ECMA-262 3rd Edition -December 1999

Hope this helps.

The Elite Gentleman