Object Literals vs. Constructor Functions

Discussion in 'Javascript' started by Corky, Aug 24, 2010.

  1. Corky

    Corky Guest

    Does one have advantages over the other. I've read that object
    literals are the preferred way of defining objects, but I'm not sure
    if that is true, or why if it is.
    Corky, Aug 24, 2010
    #1
    1. Advertising

  2. Corky

    RobG Guest

    On Aug 24, 11:20 am, Corky <> wrote:
    > Does one have advantages over the other.


    Don't assume everyone can see the subject when reading your message,
    it's always good to ensure the body of a message stands alone without
    the subject, so repeat or restate the subject in the body. Your
    question (I think) is that given a choice of creating objects using a
    literal or constructor, which has the most advantages?

    There is no definitive answer to that, you can't define one as being
    better or worse than the other in a general or sense or in absolute
    terms. They have different features, each of which might be an
    advantage, disadvantage or inconsequential depending on the the
    requirements of a particular job.


    > I've  read that object
    > literals are the preferred way of defining objects, but I'm not sure
    > if that is true, or why if it is.


    A common use of an object literal is to define an object where only
    one instance is required. Properties can be added, modified or removed
    afterward and it has a short, simple [[prototype]] inheritance chain.

    Constructors are useful for creating objects where more than one
    "instance" is required or a more complex inheritance scheme is needed
    than that provided by an object literal.

    There are other ways of creating objects, none are mutually exclusive
    and may be used in conjunction with each other. The method should be
    chosen based on the specific requirements of the object being created
    and how it should interact with other objects in the environment,
    which in turn is defined by the application architecture or program
    design.


    --
    Rob
    RobG, Aug 24, 2010
    #2
    1. Advertising

  3. Corky

    Corky Guest

    On Aug 23, 11:06 pm, RobG <> wrote:
    > On Aug 24, 11:20 am, Corky <> wrote:
    >
    > > Does one have advantages over the other.

    >
    > Don't assume everyone can see the subject when reading your message,
    > it's always good to ensure the body of a message stands alone without
    > the subject, so repeat or restate the subject in the body. Your
    > question (I think) is that given a choice of creating objects using a
    > literal or constructor, which has the most advantages?
    >
    > There is no definitive answer to that, you can't define one as being
    > better or worse than the other in a general or sense or in absolute
    > terms. They have different features, each of which might be an
    > advantage, disadvantage or inconsequential depending on the the
    > requirements of a particular job.
    >
    > > I've  read that object
    > > literals are the preferred way of defining objects, but I'm not sure
    > > if that is true, or why if it is.

    >
    > A common use of an object literal is to define an object where only
    > one instance is required. Properties can be added, modified or removed
    > afterward and it has a short, simple [[prototype]] inheritance chain.
    >
    > Constructors are useful for creating objects where more than one
    > "instance" is required or a more complex inheritance scheme is needed
    > than that provided by an object literal.
    >
    > There are other ways of creating objects, none are mutually exclusive
    > and may be used in conjunction with each other. The method should be
    > chosen based on the specific requirements of the object being created
    > and how it should interact with other objects in the environment,
    > which in turn is defined by the application architecture or program
    > design.
    >
    > --
    > Rob


    Stefan and Rob,

    Thank you both very much for your clearly written and carefully
    reasoned responses to my question. I very much enjoyed reading them. I
    apologize for the vagueness of my question. I have recently become
    very interested in JavaScript, and I've read a couple of books and
    many articles on the language. I'm at the point now of wanting to gain
    more insight into the practical applications of closures,
    objects, prototypes, and inheritance. Your vigorously effective and
    articulate comments are most appreciated.

    Thanks again,
    Bob
    Corky, Aug 24, 2010
    #3
    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. John Goche
    Replies:
    8
    Views:
    16,455
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,222
  4. java
    Replies:
    8
    Views:
    94
    Michael Wojcik
    Apr 19, 2008
  5. Oltmans
    Replies:
    2
    Views:
    83
Loading...

Share This Page