# Trying to understand prototypes

**URL:** <https://forum.kirupa.com/t/trying-to-understand-prototypes/38566>\
**Category:** Uncategorized\
**Created:** [December 25, 2003, 1:58pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566 "2003-12-25T13:58:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kO2n](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ko2n/32/248_2.png) [@kO2n](https://forum.kirupa.com/u/kO2n)\
**Post date:** [December 25, 2003, 1:58pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/1 "2003-12-25T13:58:10Z")

</div>

Whats the difference between the following two bits of code:

[AS]  
Dog = function( )  
{  
this.legs = 4;  
}  
rover = new Dog( );  
fido = new Dog( );  
yeller = new Dog( );  
[/AS]  
AND  
[AS]  
Dog = function( ){}  
Dog.prototype.legs = 4;

rover = new Dog( );  
fido = new Dog( );  
yeller = new Dog( );  
[/AS]

Is one better than the other? If so, how?  
Can’t get my head around it!?

Viru.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 2:26pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/2 "2003-12-25T14:26:54Z")

</div>

```auto

Dog = function( )
{
        this.legs = 4;
}
rover = new Dog( );
fido = new Dog( );
yeller = new Dog( );

```

Using that, you give each instance of Dog a property ‘legs’. Each dog has it’s own independant legs property. However, using

```auto

Dog = function( ){}
Dog.prototype.legs = 4;
rover = new Dog( );
fido = new Dog( );
yeller = new Dog( );

```

this, you are setting a property for all instances of dogs. Here, it’s not each dog that has the property, the property is shared for all dogs and it’s placed in the prototype object of the class constructor (see it as Shared Documents for each instance of the class). If you change this value, it will change for all dogs.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 2:30pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/3 "2003-12-25T14:30:28Z")

</div>

Thanks!

So with the second snippet is this possible:

fido.legs = 2;

While the others remain at 4?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 2:51pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/4 "2003-12-25T14:51:35Z")

</div>

No, with the first. The first one gives a seperate property to each instance. Changing one of them will not change any others. The second has one value for all instances. Change that value, and it will change for all.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 2:55pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/5 "2003-12-25T14:55:26Z")

</div>

So using the second, the values cannot be changed seperately?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 3:21pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/6 "2003-12-25T15:21:29Z")

</div>

Correct, because it’s one value for all instances.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 4:27pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/7 "2003-12-25T16:27:18Z")

</div>

> \*Originally posted by Voetsjoeba \*  
> \*\*Correct, because it’s one value for all instances. \*\*

well, not necessarily. Using

```auto
fido.legs = 2;

```

even though legs is defined in a prototype, will still give fido 2 legs with the remaining continuing to have 4. What happens is fido gets its own property in that definition. The prototype value doesnt change.

```auto
Dog = function( ){}
Dog.prototype.legs = 4;
rover = new Dog( );
fido = new Dog( );
yeller = new Dog( );

fido.legs = 2;

trace(rover .legs); // traces 4 from prototype
trace(fido.legs); // traces 2 from instance
trace(yeller.legs); // traces 4 from prototype

```

the instance value assigned to fido over-rides the value given to it from the prototype. It has precedence. The prototype serves as a reserve, or a backup value to what should be used if the instance didnt have its own property by that name. rover and yeller didnt have their own legs property so the prototype value was given. fido, on the otherhand, was assigned its own legs property of 2 so that will be used when legs is accessed from the fido instance.

So yes, they CAN be changed separately. Though, its more of a created (and then changed if altered for a second time). Not having the assignment in the constructor doesnt dissallow that instance from having its own property. The prototype serves more as a ‘default’ you could say. If the property isnt specific to that instance - if it doesnt have its own, then use the prototype value which is shared between all instances of that class. In treating the prototype in this manner, you can also easily revert to the ‘default’ value without even knowing what it is just by deleting the instances unique value of that property.

```auto
Dog = function( ){}
Dog.prototype.legs = 4;
fido = new Dog( );
trace(fido.legs); // traces 4 from prototype

fido.legs = 2;
trace(fido.legs); // traces 2 from instance

delete fido.legs;
trace(fido.legs); // traces 4 from prototype

```

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 6:02pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/8 "2003-12-25T18:02:42Z")

</div>

Simple question:

Are we defining an object here? Is dog an object? Im just trying to understand what an object is and how to use it!!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 6:29pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/9 "2003-12-25T18:29:16Z")

</div>

Dog is the class, and fido, rover and yeller are instance objects of the Dog class. Classes are objects also, everything boils down to an object.

Thank you Voet and Sen you’ve both been most helpful.

Regards,  
Viru.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 6:44pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/10 "2003-12-25T18:44:39Z")

</div>

Sen – I understood the question in a different way, thanks for completing 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2003, 7:39pm UTC](https://forum.kirupa.com/t/trying-to-understand-prototypes/38566/11 "2003-12-25T19:39:02Z")

</div>

thanks kO2n…
