Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Object Literals vs. Constructor Functions (http://www.velocityreviews.com/forums/t941904-object-literals-vs-constructor-functions.html)

Corky 08-24-2010 01:20 AM

Object Literals vs. Constructor Functions
 
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.

RobG 08-24-2010 03:06 AM

Re: Object Literals vs. Constructor Functions
 
On Aug 24, 11:20*am, Corky <bobc.corco...@gmail.com> 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

Corky 08-24-2010 03:55 AM

Re: Object Literals vs. Constructor Functions
 
On Aug 23, 11:06*pm, RobG <rg...@iinet.net.au> wrote:
> On Aug 24, 11:20*am, Corky <bobc.corco...@gmail.com> 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


All times are GMT. The time now is 05:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.