4

I thought every prototype should be an object.

Why?

console.log(Array.isArray(Array.prototype)); // true

developer.mozilla.org explains nothing.

5
  • 9
    Arrays are objects Commented May 14, 2014 at 10:42
  • @Quentin and what? if you would say "objects are arrays" it does make sense, else it not Commented May 14, 2014 at 10:44
  • I would say that array is an array. The prototype of it is an array, where is the problem ? Commented May 14, 2014 at 10:45
  • 2
    @VladimirStarkov — All arrays are objects. Not all objects are arrays. Commented May 14, 2014 at 10:46
  • 1
    @nicolallias i don't understand from that answer why array.prototype is array and not object Commented May 14, 2014 at 10:51

2 Answers 2

5

Your assumption that every prototype is an Object is not correct.

console.log(String.prototype)
console.log(Number.prototype)
console.log(Boolean.prototype)
console.log(Array.prototype)
console.log(Object.prototype)

Output:

String {}
Number {}
Boolean {}
[]
Object {}

From the ECMAScript Language Specification - 15.4.4 Properties of the Array Prototype Object (emphasis mine)

The value of the [[Prototype]] internal property of the Array prototype object is the standard built-in Object prototype object (15.2.4).

The Array prototype object is itself an array; its [[Class]] is "Array", and it has a length property (whose initial value is +0) and the special [[DefineOwnProperty]] internal method described in 15.4.5.1.

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

7 Comments

it is an fact, but why?
@VladimirStarkov Because that's what the spec dictates. A question to you, why not? Why do you expect it to be Object?
can you give me a link to this spec?
@Stijn Because it would be less confusing if it were not an array and just an object, there has to be a reason for it. Those guys just don't go around designing stuff just because "why the hell not ?".
It is not specific to arrays though, functions are like that as well. In the specs, the reason is said to be: 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. (this also applies to arrays) That's the why. However, it leads to another why, "Why did ECMAScript code that was created prior to the ECMAScript 2015 specification have Function.prototype as an empty function ?". It is all disappointing really, things should be clearer than that.
|
-1

Try typing this into your javascript console: typeof Array.prototype;

The Array.prototype is actually an Array. Which is detailed on this page.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype

It probably has something to do with the Fact that [] is shorthand for Array.

So Array.prototype points to []. Array.prototype.constructor points to function Array() { [native code] }

[].constructor also points to function Array() { [native code] }

So at a guess, it is done this way so that you can user Array and [] interchangably.

I dont know for sure this is the reason, but thats my best guess.

6 Comments

your answer explains nothing
@VladimirStarkov I have fixed that. It now explains something. I should have done that from the start. Thanks for calling me out and making me put an actual answer.
it is strange to me that "Which is detailed on this page" is equal to "Little known fact:"
Well .. it IS detailed on the page. No reason for it to be strange.
Yes, but there is no explanation why it is so
|

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.