View Full Version : What does "prototype" do ?

02-20-2003, 05:15 PM
as in

String.prototype.functionName = functionName;

and similair

02-20-2003, 05:30 PM
You extend constructors that way.


var myStr = "hello world!";

String.prototype.reverse = function() {
return this.split('').reverse().join('');


Basically, an object will inherit all methods and properties in its constructor's prototype. Even if the method is added after the object has been initialized, it still gets it,

That's why Mozilla rocks, you can do stuff like:

HTMLDivElement.prototype.bla = function() {
// do something?

And now you can do document.getElementById('someDivID').bla()


02-20-2003, 06:04 PM
In Actionscript (nearly like Javascript), I did extensive research on this question. One thing that's true there: you should avoid using prototype unless it is necessary, as it eats up memory resources, or something like that. The gurus told me.

I do not know enough yet about Javascript to imagine a case where it is necessary, though the need is obvious enough in Flash, where a target path to a function cannot always easily be established. In that case, it makes sense to use it and they often do - just don't use it for EVERY function.

Hope that helps.

02-20-2003, 06:45 PM
I don't see why using prototypes would be inefficient. Every property and method call should look locally, then up the prototype chain until it finds the correct thingy.

The Javascript 1.5 description of prototype:

02-20-2003, 06:55 PM
As I understood it, in Flash, at least, all properties of prototyped objects are stored in active memory, so if it is not necessary to prototype an object, then they put it into a regular old function. This may not be relevant in javascript, but then again, it might.

Also it probably only matters when you are doing processor-intensive functions. I've seen pages in Flash that, even with expert coding, still took 10 seconds to load, because the processing was so complex. Took us nearly a year to do that 10 seconds, lol! In that case, these things can be very important.

: )

02-21-2003, 11:18 AM
Originally posted by jkd
You extend constructors that way. [etc..]

Damn that's cool! Thanks man - I'll make good use of that :thumbsup:

I can see how the lack of general mozilla useage must drive you insane with frustration at times ... :rolleyes:

02-21-2003, 08:23 PM
What does constructors do? I realize you can only understand prototype along with this, but to me, it's like the chicken and the egg. One is mentioned with the other and I need an understanding of both, and I can't learn one at a time, can I?

02-22-2003, 03:12 AM
This part...

var myStr = "hello world!";

... is a String, and String is a constructor. Therefore...

String.prototype.reverse = function()
return this.split('').reverse().join('');

... would add this particular function to the myStr string automatically. You'd call up the functionality like this...

... or even...
"Hello World!".reverse();

You can probably do a search on http://devedge.netscape.com for "constructors" for a list of more of them.

Make sense? :thumbsup:

02-22-2003, 01:18 PM
Hey, that's cool. I'm going to be using this a lot...

02-22-2003, 04:14 PM
This is a powerful property and I've used it to shorten problems where global arrays and all sorts of nonsense would have had to be used.
Any object type (core and user-defined) can be done in IE and Netscape 4.x.
As jkd said, Mozilla prototypes even HTMLElements(and all subsets, basically anything that's an Object) so that contrary to popular belief, Mozilla can emulate nearly anything IE does - outerHTML, whatever - with a prototype set up in the head. And of course it goes beyond IE with getters and setters.

Anyway, back to standard prototyping, I've even prototyped Number object to shorten a double array sort problem(totally cross-browser, well IE/NN anyway). Basically it's excellent for shortening and simplifying problems and coding . Also, gives objects the power to 'own' a simple property(even a String) and so cuts need for global arrays and all sort of error-prone coding.

02-22-2003, 06:45 PM
I'm not sure if I get it. It sounds alot like typeof

02-22-2003, 07:41 PM
typeof is also a little bit unknown, but only serves real purpose if used to test against 'undefined'. That's my opinion.
Whether it's a string, number or whatever, should be dictated by your coding. That's the Java programmer in me speaking. So, only a :
if (typeof myVariable=="undefined") is worth a lot in my eyes.
prototype is much more powerful. OK, you could go throughout your life without ever using it, but if you are in need of some object property definition or even code simplification by defining certain objects to possess some method, then prototype is the way to go.

02-22-2003, 07:44 PM
Do you mean, you can somehow create custom methods on functions?

02-22-2003, 08:16 PM
Errr.... not 100% sure what you mean. Possibly. Give a small example of what you were thinking of(pseudo-code if necessary).

02-22-2003, 08:31 PM
I mean, something like

function puzzle()
// some prototype junk that I don't understand does in here

if (puzzle().complete){alert()}

02-22-2003, 08:57 PM
Prototyping is the method by which javascript can be extended into an OOP language and add public methods to object constructors. I have some great links that I would like to share:

JavaScript Tutorial: Functions and Classes:

JavaScript Object-Oriented Programming Part 1

JavaScript Object-Oriented Programming Part 2

Class-Based vs. Prototype-Based Languages

Private Members in JavaScript

- Terry

02-23-2003, 02:14 AM
Thanks, I will be looking into those, especially since they correspond to what I'm learning in C#...