Literal Notation Vs. Constructor Notation

C++, C#, Java, PHP, ect...
Post Reply
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Literal Notation Vs. Constructor Notation

Post by Mardonis »

Which would you prefer and why? Does the code run any faster using one over the other? Also is any one over the other used more as a standard?

Thanks
Mardonis
User avatar
a_bertrand
Posts: 1536
Joined: Mon Feb 25, 2013 1:46 pm

Re: Literal Notation Vs. Constructor Notation

Post by a_bertrand »

It depends what you want and which language you use. For JS, literal notation let you quickly create structures / objects. In C# it's called anonymous classes and is used specially in conjunction with LINQ / lamda expression.

I must say I do use it a lot specially in JS. In C# I use it just for LINQ (or nearly only for LINQ) as it has some higher drawbacks.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Literal Notation Vs. Constructor Notation

Post by Jackolantern »

I almost always go for literal notation in JS. There a few bugs with constructor notation, primarily concerning inheritance. Reading any of the JS inheritance articles or books from Douglas Crockford would outline why literal notation is typically considered better, mostly due to inadequacies of JS.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Literal Notation Vs. Constructor Notation

Post by Xaleph »

It`s personal preference really, in JS that it is. I for example like to construct objects, not because I want to, but because the literal notation would kill me after xxx lines of code. As far as I know, most bugs have been taken care of and many decent implementations exist. When it comes to inheritance ( we`re prototyping really) it tends to get tricky. However, many people have already created solutions for this. I for example use a modified ( and inherently better ) approach based on John Resig`s classical constructor. It takes away some of it`s drawbacks with "static" and public/private.

In conjunction with this, I use a modular approach to code. Classes are all namespaced and contained within the namespace. So there`s room for private fields as well. If I want, I can "globalize" any and all classes I want without sacrificing the private fields. However, despite all of this, I can still instantiate ( thus; constructor notation) objects and functions.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Literal Notation Vs. Constructor Notation

Post by Jackolantern »

The bugs are still there, and will never be fixed because they aren't actually bugs. They are just piss-poor decisions in the specification. The biggest is the changing of the constructor object when you are doing inheritance with constructor functions. Forgetting to reset the constructor will cause catastrophic issues in your inheritance hierarchy. Some people overcome this by creating their own inheritance methods to set everything needed. But there are still other issues.

Really, for large projects the best is the module pattern, and almost all major JS projects (node.js, meteor, etc.) use it in some form either covertly or overtly.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Literal Notation Vs. Constructor Notation

Post by Xaleph »

Yarr, I agree, the modular approach is a very good approach currently. It is well documented, many libraries use it and it`s quite easy to work with. When in doubt, look what the big boys do :)
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: Literal Notation Vs. Constructor Notation

Post by Mardonis »

Thanks for all the responces guys. I'll look into the modular stuff.
Alysha50
Posts: 6
Joined: Tue Nov 26, 2013 2:45 pm

Re: Literal Notation Vs. Constructor Notation

Post by Alysha50 »

I know bout an online tool that can help you guys to compare both bands based on different factors. Just go on http://www.ninki.co and type in "Literal Notation Vs. Constructor Notation" and you'll get an exact idea which one is better
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Literal Notation Vs. Constructor Notation

Post by Jackolantern »

Alysha50 wrote:I know bout an online tool that can help you guys to compare both bands based on different factors. Just go on http://www.ninki.co and type in "Literal Notation Vs. Constructor Notation" and you'll get an exact idea which one is better
Maybe I am not awake yet, but I can't quite figure out how to use that site. I type it in, hit enter, and it keeps bringing up the words I searched for with a circle and a number in it. What is this?
The indelible lord of tl;dr
Post Reply

Return to “Coding”