Javascript constructors vs prototypes

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
After having my ass handed to me on a brainbench Javascript test i decided to read a little bit more on the differences between JS and traditional OOP languages.

As i understand it, JS uses constructors often because it does not instantiate objects from strongly typed classes. Thus, it uses the constructor to create local variables and methods.

Prototyping, on the other hand, works with constructors to do essentially the same thing, but to store common variables/methods in a centralized object called the Prototype. This all makes sense except that it seems horribly redundant to have two different programmatic constructs for the same purpose

The only reason i can see to have prototypes are:

a) saves memory since common methods and properties/variables are stored only once, and not multiple times (unless overridden).

b) If you are a crappy programmer and like to change class definitions at runtime (after objects have been instantiated from them), you can do that with prototypes.

c) Allows for common methods/properties to be used by all objects of the prototype without having to have the constructor create separate ones for each instance of the object. (see #1).

Is this all there is to it? That doesnt sound like much of an advantage to me over just using the constructor more often...
 

DJFuji

Diamond Member
Oct 18, 1999
3,643
1
76
eh...sounds kind of like a useless construct to me. You could do the same thing by declaring global variables...albeit it wouldn't be a very eloquent solution...