Object Literals vs. Constructor Functions

C

Corky

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.
 
R

RobG

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.
 
C

Corky

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top