defining classes - perferred approach

Discussion in 'Javascript' started by mca, Jul 27, 2006.

  1. mca

    mca Guest

    this is a bit long-ish post regarding class defnitions in javascript.

    i am interested to know how most folks prefer to define classes in
    javascript. i know there are a handful of very good 'frameworks' for
    handling oo-type js code. i am currently interested in exploring the
    internal details of javascript/ecmascript in this matter, tho.

    i have seen two primary approaches for defining classes in javascript.
    for the sake of argument i will call them "enclosed" and "prototype"
    here's an example:

    // enclosed approach
    someClass = function()
    {
    this.methodName = function()
    {
    return "methodName";
    }
    }

    // prototype approach
    anotherClass = function()
    {
    }
    anotherClass.prototype.methodName = function()
    {
    return "methodName";
    }

    i find that the choice of this approach has implications for
    'overriding' patterns. for example, if you use the "enclosed" approach,
    you cannot inherit from the class and then use the "prototype" approach
    to override the base class method. however, you *can* inherit from the
    the base class and use the same "enclosed" approach to successfully
    override the method.

    i also find that the "enclosed" method is very friendly when dealing
    with private variables of the class, but that the "prototype" approach
    is less so.

    i am interested in other's views on these two approaches. are there
    traps here? are there other, more reliable approaches than these? is
    one better suited for publicly-shared libraries and another better for
    personal/internal use?

    tia for your feedback.

    mca
    mca, Jul 27, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    3
    Views:
    10,817
    AlecB
    Jul 7, 2006
  2. Replies:
    2
    Views:
    8,997
    Heinz Ozwirk
    Aug 9, 2006
  3. howa
    Replies:
    2
    Views:
    94
    Richard Cornford
    Apr 15, 2007
  4. howa

    Perferred way to create object

    howa, Jul 4, 2007, in forum: Javascript
    Replies:
    2
    Views:
    75
  5. howa
    Replies:
    2
    Views:
    89
    scripts.contact
    Jul 26, 2007
Loading...

Share This Page