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...
			
			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...
 
				
		 
			 
 
		 
 
		 
 
		 
 
		
 Facebook
Facebook Twitter
Twitter