2

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 ?

asked Sep 26, 2016 at 9:16
3

4 Answers 4

4

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

answered Sep 26, 2016 at 9:18
Sign up to request clarification or add additional context in comments.

2 Comments

if it is a function object ,can i invoke it like Function.prototype() ?
@AL-zami: You can indeed; as the quotes above say, all it does is return undefined.
1

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.

answered Sep 26, 2016 at 9:18

1 Comment

I think OP just means "object" when they say "object literal". Just like how many people say JSON object, they don't actually mean it, it's just a lack of terminology knowledge
1

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.

I also posted this on LinkedIn.

General Grievance
5,12039 gold badges40 silver badges60 bronze badges
answered Jul 10, 2022 at 5:54

Comments

-1

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.

answered Sep 26, 2016 at 9:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.