SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.


    Matt

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    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')

  3. #3
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    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: http://www.crockford.com/javascript/inheritance.html. (I used "classic inheritance" in quotes because JavaScript by it's very nature uses Prototypal inheritance)

    The Mozilla Developer Network has an Introduction to Object-Oriented JavaScript that could be helpful as well :-)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  4. #4
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  5. #5
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by johnuk View Post
    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: http://getfirebug.com/logging and http://code.google.com/chrome/devtoo...s/console.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The console object is a browser-based object that allows you to log messages to a debugging window.
    See: http://getfirebug.com/logging and http://code.google.com/chrome/devtoo...s/console.html
    Great, thanks Paul.

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

    http://snipt.net/geekyjohn/basic-ins...bject-example/

    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 Im used to inheritence in Java and I liked how easy it was (similar to Java) with the prototype Library.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by johnuk View Post
    How would I extend the 'Animal' Javascript class in geeky johns article ? an example would be really helpfull
    Code javascript:
    function Dog(location) {
        return new Animal('Dog', location);
    }
    pet2 = new Dog("Yard");

    Quote Originally Posted by johnuk View Post
    Im used to inheritence in Java and I liked how easy it was (similar to Java) with the prototype Library.
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •