Search code examples
javascriptprototype

Javascript - Getting 'undefined' when trying to get array's prototype


I think its a 5am brain drain, but I'm having trouble with understanding this.

obj = ['a','b'];
alert( obj.prototype ); //returns "undefined"

Why isn't obj.prototype returning function Array(){ } as the prototype? It does reference Array as the constructor.


Solution

  • Because the instance doesn't have a prototype, the class* does.

    Possibly you want obj.constructor.prototype or alternatively obj.constructor==Array

    * to be more accurate, the constructor has the prototype, but of course in JS functions = classes = constructors