Trying to understand prototypes

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.


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


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.

Thanks!

So with the second snippet is this possible:

fido.legs = 2;

While the others remain at 4?

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.

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

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

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

well, not necessarily. Using

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.

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.

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

:slight_smile:

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!!

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.

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

thanks kO2n…