What are javascript prototypes? (longer answer)

[I’ve also written the short answer (for advanced javascripters)]

Javascript is using prototypes and is the only language I know that is doing it. What is the idea behind it? Simple. With prototypes, you can extend (add methods/properties) any class you want anywhere you want anytime you want even if you are not the owner of that object. Object-oriented purist will be shocked but I am more than pleased with that.

Why using prototypes?

It’s memory-friendly
By adding a method to a class prototype, you are creating a single occurence of the function that is referenced by every objects of that type.

It’s easy

To add a method to a class, no need to create a new class. Juste write TheClass.prototype.theMethod = function() {//code here} and it’s done.

It’s fun!
Maybe not as fun as drinking kool-aid but compared to the complexivity of other languages, we have a champ.

How to use prototypes

Simple. Write the [NameOfTheClass].prototype.[NameOfTheExtension].

You want to add a trim function to the String object? (thanks Greg for the correction)

[source:javascript]
String.prototype.trim = function()
{
return this.replace(/^\s*|\s*$/g, “”);
}
[/source]

You want to add an oldValue property to the string

String.prototype.oldValue = “the old value”;

Beware! All the new String objects and the one already declared will have a property with the value “the old value” inside. I can’t think of a particular reason why one would do this. If you have one, please comment.

Commentaires

  1. Greg Knapp March 26 2007 at 05:40:16

    Hi,

    Does the trim function you posted clear all whitespace - inc spaces between words? This would emulate trim behaviour?

    String.prototype.trim = function() {
    return this.replace(/^\s*|\s*$/g, “”);
    }

    I think Ruby emulates this behaviour as well, you can open any class (inc language base classes) and add methods.

  2. Dan (maintainer of Javascript Kata) March 26 2007 at 09:04:37

    Greg, this trim function clear all whitespaces before and after the string.

    ” TEST ” becomes “TEST”.

    How can you extend objects this way in ruby? (by the way, have a look at my friend site : http://www.rubyfleebie.com/)

  3. Hiram Abiff April 29 2007 at 19:46:00

    For Ruby:

    s = ” string ”

    class String
    def trim2 #trim already exists
    self.gsub(/^\s*|\s*$/, ”)
    end
    end

    s.trim2 # => “string”

  4. Christopher Roach August 13 2007 at 15:00:43

    Hi Dan,

    In your first paragraph you mention that Javascript was the only language that you know of that uses prototypes. Well, I just wanted to point out that, while Javascript makes good use of the prototype mechanism, it is by far not the only, nor the first, language to do so. The first language to introduce this concept was the Self language. Other languages today still use prototype method to perform OO. The two languages, other than Javascript, that I am most acquainted with are Io and Lua. Take a look at the Wikipedia article on Prototype-based Programming (http://en.wikipedia.org/wiki/Prototype-based_programming) and you’ll no doubt find some other really interesting languages that do OO but don’t use the traditional class-based methodology.

    Cheers and keep up the good work,

    Christopher

  5. Dan (maintainer of Javascript Kata) August 13 2007 at 15:11:32

    Thanks for your comment! It’s appreciated

Post a comment

Comments are moderated and innapropriate ones won't be approved. Please respect this public space.