Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Object Literals vs. Constructor Functions

Reply
Thread Tools

Object Literals vs. Constructor Functions

 
 
Corky
Guest
Posts: n/a
 
      08-24-2010
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.
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-24-2010
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
 
Reply With Quote
 
 
 
 
Corky
Guest
Posts: n/a
 
      08-24-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about function objects vs object literals? Oltmans Javascript 2 06-30-2008 07:51 PM
Recursive object literals in Javascript ? java Javascript 8 04-19-2008 04:25 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Java: byte literals and short literals John Goche Java 8 01-17-2006 11:12 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57