Prototype Class and Error Catching

Hey All,

 I am very interested to start using the Prototype library to write OO Code in the hope to eventually switch from my usual procedural style when doing medium to large projects.

I have just read the section on using Classes on the prototype website, and Im impressed with what Ive read. The only thing is Im wondering how would I handle a situation such as ‘invalid’ variables being passed to a constructor?

Say I wrote an app which required a variable with the value of 100 or 200, and failing that condition the program must fall over and inform the user why?

I hope that made sense! thanks.


In the initialize method, you’d use an if statement to test if the variable has an appropriate value, and if it doesn’t, then you can throw new Exception(‘with a problem description’)

I find with a lot of projects I write, I can usually get away with only one or two classes, so I tend to use templates like this “basic instantiable object” to get me through the day.

Though if you wanted to get some classes happening with “classic inheritance” then you could try some of the things Douglas Crockford is suggesting: (I used “classic inheritance” in quotes because JavaScript by it’s very nature uses Prototypal [URL=“”]inheritance)

The Mozilla Developer Network has an Introduction to Object-Oriented JavaScript that could be helpful as well :slight_smile:

Thanks Jeff that worked a treat. And thankyou Aussie John for your very usefull input, Im going to have a good read through those links.

I have also considered that one or two classes will be enough for most projects, I just noticed that recently my procedural code whilst neat is becoming a little unyealding.

Ive spent the morning writing some test scripts with the Prototype Library and I have to say it works really well!

One thing John, in that ‘Basic Instantiable Object’ example whats with the reference to ‘console’ ? I cannot for the life of me work out what it references.

The console object is a browser-based object that allows you to log messages to a debugging window.
See: and

Great, thanks Paul.

Ok so ive adopted the approach of the ‘Basic instantiable Object’ shown here:-

I have created a working class, and im pleased with the results. But im struggling to work out how to get some inheritance going after having read the other articles.

How would I extend the ‘Animal’ Javascript class in geeky johns article ? an example would be really helpfull :wink: Im used to inheritence in Java and I liked how easy it was (similar to Java) with the prototype Library.

function Dog(location) {
    return new Animal('Dog', location);
pet2 = new Dog("Yard");

Anything that you might be used from Java in terms of classes and methods, you will find that JavaScript has no classes or methods.

Here’s a good talk from Douglas Crockford all about functions, class-based things and prototypes, that helps to explain most of this.
Crockford on JavaScript - Act III: Function the Ultimate

Brilliant thanks!