1

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.

5 Answers 5

7

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

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

7 Comments

Nah, that isn't it either. alert( obj.constructor.prototype ); returns nothing.
no, it returns the prototype.toString() - notice how this isn't null, and is not the same thing as returnign nothing - that is the correct reference, what you're doing with it may not be
Ok, so I think I'm understanding. So obj = 'a', obj = {a:'a'}, obj = ['a'], obj = 0 have no prototypes because they are instances. But, obj = function(){} returns "[object Object]" for its prototype
because function is a constructor so it has a prototype - functions are basically the only things you'll get a prototype reference on, whether those are native functions or your own
long story short - functions are special in JS :D
|
1

I'm not sure you can access the prototype object from an instance of an object. The following behaviour might help you:

alert(Array); // displays "function Array() { [native code] }"
alert(Array.prototype); // displays ""
alert(['a','b'].constructor); // displays "function Array() { [native code] }"

obj.prototype isn't returning function Array() { ... } as that is the object's constructor.

1 Comment

‘constructor’ isn't really reliable; it's not present on IE, and has a subtly different behaviour to what it looks like it's doing, which breaks many forms of subclassing. Best avoided if possible!
1

In your example, obj is an instance of an Array, not the class Array itself.

Another way to understand it is that for example, you can't inherit from an instance of an object (or class), you can only inherit from the object (or class) itself, which in your example it means that you could inherit from the Array object, but not from a direct instance of the Array object such as obj.

1 Comment

Thanks. I tested the idea against other object types and also got the 'undefined' response, so I'm understanding that. The only one that is different is the case of: obj = function(){} This returns [object Object]
1

According to the ECMA spec, an object's prototype link isn't visible, but most modern browsers (firefox, safari, chrome) let you see it via the __proto__ property, so try:

obj = ['a','b'];
alert( obj.__proto__ );

An object also has the `constructor' property set on construction, so you can try:

obj = ['a','b'];
alert( obj.constructor.prototype );

However, obj.constructor can be changed after an object's contruction, as can obj.constructor.prototype, without changing the actual prototype pointer of obj.

Comments

0

Not really my cup of tea, but does this way of defining it make "obj" an array? Tried

obj = new Array();
obj[0] = "a";
obj[1] = "b";

?

2 Comments

Tried that too, same thing as the array literal
An array literal is just a more convenient way of creating an array: developer.mozilla.org/en/A_re-introduction_to_JavaScript#Arrays

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.