2

I created a Car object like this.

var Car = Object()

Then I added some properties to it like

Car.price = 5000;
Car.color = "red";

Then I created an instance of it like

var car1 = Object.create(Car);

What I don't understand is, when I call car1.prototype or Car.prototype, I get undefined.

According to what I've read, every Object is a prototype of something.

Can someone explain why it's "undefined"?

2 Answers 2

5

every Object is a prototype of something

No. Every object has a prototype (except the root object). However, only functions have a prototype property.

The value of that property will become the prototype of the objects that are created by calling the function as constructor function (e.g. var obj1 = new Constr();).

What I don't understand is, when I call car1.prototype or Car.prototype, I get undefined.

As just explained, only functions have a prototype property. car1 and Car are objects and therefore don't have such a property.

To get the prototype of an arbitrary object, you can use Object.getPrototypeOf:

var car1Prototype = Object.getPrototypeOf(car1);

Then I created an instance of it like var car1 = Object.create(Car);

You didn't create an instance of Car. If you created a new object and explicitly set its prototype to Car. Alternatively you could have defined Car as constructor function:

function Car(price, color) {
    this.price = price;
    this.color = color;
}

var car1 = new Car(5000, 'red');

Now that Car is a function, Car.prototype would yield the prototype of car1, i.e.

Car.prototype === Object.getPrototypeOf(car1);

See also Introduction to Object-Oriented JavaScript

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

Comments

2

Functions have prototypes. Object instances, like car1, don't.

It's correct to say that every object is a copy of some prototype. By default, objects are a copy of the prototype of the Object "function". However, object instances don't have a prototype themselves.

Think of a prototype as a "model" for a new Object. In Object Oriented languages, a class or a struct act like a strict model for new instances. That is, every instance of the Car class necessarily have motor, for instance. Likewise, in JavaScript, function prototypes act like the "model" for new instances of that function, but they are not strict. Once you create an instance from that model, you can add and remove members, because the language is dynamic.

EDIT

As Pointy clarified, it seems that object instances are not a copy of the "model" function prototype property. It seems that the prototype is copied to an internal object that is looked up when you try to access a property of the instantiated object.

Example:

var car1 = new Car();
// car1 now is not a copy of Car.prototype, but it has an internal object that is.
car1.motor = new V8Motor();
// even though motor is not a property of car1, this will work because JavaScript will find this property in the car1 prototype as it's not a member of the instance itself

4 Comments

Uhh ... that's not how prototypes work in JavaScript. The prototype is not copied to new objects.
How is it not copied?
The prototype reference is copied from the constructor function to an internal property of a newly-constructed object. When a property lookup is conducted, the prototype chain is consulted if the object does not directly have a property of a particular name. Here is a good blog post on the topic.
Your edit is still incorrect. Nothing is copied, the new object simply gets a reference to Car.prototype. Also, assigning car1.motor does not have anything to do with the prototype. It simply creates the property on the object.

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.