3

I thought every prototype should be an object.

why?

Array.isArray( Array.prototype ) // true

developer.mozilla.org explains nothing

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Vladimir Starkov
  • 19,264
  • 8
  • 60
  • 114

3 Answers3

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.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • it is an fact, but _why?_ – Vladimir Starkov May 14 '14 at 10:50
  • 2
    @VladimirStarkov Because that's what the spec dictates. A question to you, why not? Why do you expect it to be `Object`? – user247702 May 14 '14 at 10:54
  • can you give me a link to this spec? – Vladimir Starkov May 14 '14 at 10:59
  • @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 ?". – doubleOrt Oct 26 '17 at 19:44
  • @Taurus looking back, I don't think this is a good answer. Unfortunately we can only speculate. It may have something to do with arrays not being included in the first prototype of JS, which was designed in 10 days ([source](https://ponyfoo.com/articles/standard)). – user247702 Oct 26 '17 at 20:28
  • 3
    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. – doubleOrt Oct 26 '17 at 20:34
  • @VladimirStarkov Here's the spec: https://www.ecma-international.org/publications-and-standards/standards/ecma-262/. Note though that the spec is not easy to read. It is not written from the point of view of defining the behaviour of the language. Instead it is written from the point of view of someone wanting to implement javascript in C++. Part of this is because the spec was written mainly for the benefit of web browser developers. Part of it is because the spec evolved from the original implementation of javascript in Netscape Navigator. – slebetman May 22 '23 at 15:11
0

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.

iLikePrograms
  • 470
  • 2
  • 9
-1

But just trying to contribute however I can.

The prototype is meant to add on functionality/capabilities to all Javascript objects, of which Array is a Javascript object. However, arrays are a special kind of object.

If you checked the typeof for Arrays, it will reflect as object. (Source 1)

However, end of the day, you should think of them as Arrays ([]) and not objects per se ({}). So an array is a special kind of object, and because it's a JS object, it has access to prototype which allows it to have access to new methods and properties. (Source 2)

This is based on my rudimentary research and understanding.

Harsha Murupudi
  • 579
  • 1
  • 6
  • 19
omgabear
  • 1
  • 1