Closed Thread Icon

Topic awaiting preservation: prototype ? - anyone got a simple and clear explanation? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8644" title="Pages that link to Topic awaiting preservation: prototype ? - anyone got a simple and clear explanation? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: prototype ? - anyone got a simple and clear explanation? <span class="small">(Page 1 of 1)</span>\

 
smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 05-09-2003 23:48

Hi, very quick thread,

Can anyone give a concise and clear explanation of the 'prototype' thing in javascript? I don't understand it at all.

Also why is this useful, where can this be of assistance, an example situations where someone might want to use it?

Thanks

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-09-2003 23:58

It's a way of assigning properties or methods (functions) to a whole class of objects without making a separate copy of the property/method for each instance of the object.

Without prototypes:

function myObject(a,b,c)
{
this.a=a;
this.b=b;
this.c=c;
this.dostuff = function(){...}
this.domore = function(){...}
}
var obj1 = new myObject(1,2,3);
var obj2 = new myObject(2,3,4);
var obj3 = new myObject(5,4,3);

Each object (obj1,obj2,obj3) in that example has its own separate copy of the dostuff and domore functions. This wastes memory.

With prototypes:

function myObject(a,b,c)
{
this.a=a;
this.b=b;
this.c=c;
}
myObject.prototype.dostuff = function(){...};
myObject.prototype.domore = function(){...};
var obj1 = new myObject(1,2,3);
var obj2 = new myObject(2,3,4);
var obj3 = new myObject(5,4,3);

None of the objects (obj1,obj2,obj3) in this example have their own copy of the dostuff or domore functions. Rather, if you tried obj1.dostuff(), it would say "obj1 doesn't have a function called dostuff! oh, let's look at the prototype. there's dostuff." and the prototype's dostuff would be called.

This is useful for simulating inheritance, since if the prototype is assigned to be *another* object, and that other object doesn't have the property, it'll check *that* object's prototype. For example:

// generic pet object
function Pet(name)
{
this.name = name;
}
Pet.prototype.takeToVet = function(){...};

function Dog(name, breed)
{
this.name = name;
this.breed = breed;
}
Dog.prototype = new Pet();

mydog = new Dog();
mydog.takeToVet(); // calls the function declared above

So there you have it; the advantages of prototypes.

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 05-10-2003 02:03

you legend Slime, I've never been able to figure that out before, nice one.. many cookies.

smonkey
Paranoid (IV) Inmate

From: Northumberland, England
Insane since: Apr 2003

posted posted 05-10-2003 02:22

Ok I think I get it, it is a very good explanation but I'm just a little bit on the dumb side of stupid so it may take a while before I fully understand it all. At the moment it just seems to have made things so much more complicated, and just when I was feeling good coz I nearly understand regular functions.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-10-2003 07:38

If you haven't yet worked with objects, don't worry about prototypes. Prototypes are one thing that you should study when you are studying objects. So, if you don't yet know how objects work in JavaScript, don't concern yourself with this; you'll probably just get confused. Return to it when you get into objects.

By the way, there's only one place I've ever needed to use the inheritance technique, and that was in The JavaScript Raytracer 2.0. Every shape object (sphere, box, plane, etc) inherits from a generic Obj object, which provides basic object functionality. (This is typical of raytracers.) If you look in each object's JS file, you can see the line

Sphere.prototype = new Obj();

I wonder if my scripting skills would improve if I tried to use objects more often than I do in practice.

« BackwardsOnwards »

Show Forum Drop Down Menu