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
Literal Notation Vs. Constructor Notation
- a_bertrand
- Posts: 1536
- Joined: Mon Feb 25, 2013 1:46 pm
Re: Literal Notation Vs. Constructor Notation
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.
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
Mad programmer and annoying composer
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Literal Notation Vs. Constructor Notation
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
Re: Literal Notation Vs. Constructor Notation
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.
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.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Literal Notation Vs. Constructor Notation
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.
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
Re: Literal Notation Vs. Constructor Notation
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
Re: Literal Notation Vs. Constructor Notation
Thanks for all the responces guys. I'll look into the modular stuff.
Re: Literal Notation Vs. Constructor Notation
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
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Literal Notation Vs. Constructor Notation
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?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
The indelible lord of tl;dr