why typeof(Function.prototype) is function

364 views Asked by At

I am aware of the fact that Prototypes are object literal. So methods and properties can be defined on them. Function.prototype has some method like apply, call, bind, toString etc. So I thought a function's prototype should be a object literal. But I ran following code and encountered that Function.prototype is of type function !

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

How come it is not a object literal itself ?

4

There are 4 answers

2
T.J. Crowder On BEST ANSWER

From the specification:

The Function prototype object is the intrinsic object %FunctionPrototype%. The Function prototype object is itself a built-in function object. When invoked, it accepts any arguments and returns undefined. It does not have a [[Construct]] internal method so it is not a constructor.

NOTE

The Function prototype object is specified to be a function object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification.

(my emphasis)

If we go to the ES5 spec, it says:

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

...without offering any explanation for why that would be the case. That language is essentially unchanged in ES1, ES2, ES3, and ES5. I think the original idea was basically that that was what gave it its function-ness, although typeof (even in ES1) didn't look at the internal [[Class]], it looked at whether the thing implemented [[Call]] (as it still does). When something goes back all the way to ES1, one frequently has to just invoke the "because Eich did the first JavaScript in 10 days and yeah, weird stuff happens when you do that" argument. :-)


Side note: By "object literal" I take it you mean "plain object." (An "object literal" — what the specifiation calls an object initializer — is just a way to write an object in source code. There are other ways to create plain objects.)

0
Jay Shah On

Let's say you have declared an array, let arr = [ 1 , 2 ]; So, internally it will be created as let arr = new Array ( 1, 2 ); This is the function constructor.

Have you ever thought about how the array got all functions like concate, map, filter, reduce etc.?

Internally when we create an instance from a function constructor, the prototype property of a function will be set to the prototype property of that newly created instance. Hense, this concate, map, filter, reduce get automatically associated with that function constructor. So that's how we can use that array properties by arr.map, arr.concate.

Actually the prototype property of a function is visible but the prototype property of an instance which is created by a function constructor is hidden. If you want to check then you can check it by obj_name.proto. It's a pointer towards that prototype property.

Now, you can see that the array "arr" is not the array internally. It's an instance of a function constructor. That's why if you check the type of the array, you will get the answer as object and also if you check the typeof(Array), you will get the answer as Function.

If you find it useful then please like it on https://www.linkedin.com/posts/sh-jay_javascript-array-prototype-activity-6951547190049677312-Dqbn?utm_source=linkedin_share&utm_medium=member_desktop_web

1
Quentin On

An object literal is some JavaScript syntax for creating objects. It isn't a data type.

Functions are just a specific type of object in JavaScript. Anywhere you can have an object, you can have a function.

0
TylerY86 On

Well, I don't think you mean object literal, as alluded to by other answers and comments.

alert(Function.prototype instanceof Object) // true
alert(Function.prototype instanceof Function) // true
alert(typeof Function.prototype) // function

It is an object. It's also a function. Also, all functions are objects. They're all following the rules just fine.

alert((function(){}) instanceof Object) // true
alert((function(){}) instanceof Function) // true
alert(typeof (function(){})) // function

One big happy we-all-derive-from-Object family. Why should the prototype of Function not be a function?

Now if you wanna get weird... let's get weird.

var notAFn = Object.create(Function.prototype);
alert(notAFn instanceof Function); // true
alert(typeof notAFn); // object

And no, you can't call notAFn(). Not until they add a call Symbol for that. :)

Oh hey, feel free to tell me why this isn't a good answer. I'll try to improve it.