Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Object-Oriented JavaScript is not necessarily the answer to all your problems.</p> </blockquote> <p>My advice is to be careful the choice you pick on this topic. </p> <p>In practice, OO-JS can add more complexity to your code for the sake of trying to be more like traditional object-oriented languages. As you probably know, JS is unique.</p> <p>It is important to know that there are Design Patterns that will structure your code and keep implementation light and flexible.</p> <blockquote> <p>It is Design Patterns that I see structuring advanced JS implementations, not OO. To paraphrase Axel Rauchmeyer - "Object Oriented methodology does not fit into basic JavaScript syntax, it is a twisted and contorted implementation, and JS is far more expressive with out it."</p> </blockquote> <p>The root of this analysis boils down to the fact that JS has no class. In essence, since everything is an object, you already have object-oriented variables and functions. Thus the problem is slightly different than the one found in compiled languages (C/Java).</p> <blockquote> <p>What Design Patterns are there for JavaScript?</p> </blockquote> <p>An excellent resource to check is Addy O' Somani and Essential Design Patterns. <a href="https://www.google.com/#hl=en&amp;output=search&amp;sclient=psy-ab&amp;q=addy+osmani+javascript+patterns&amp;oq=addy+osmani+javascript+patterns&amp;gs_l=hp.3..0j0i8i30.11262.24370.1.24535.39.33.4.0.0.0.656.4170.14j16j2j5-1.33.0.les;..0.0...1c.1.3nX8vl8l504&amp;psj=1&amp;bav=on.2,or.r_gc.r_pw.r_qf.&amp;fp=783e549a5df6db07&amp;biw=1134&amp;bih=907" rel="nofollow">He wrote this book on Design Patterns in JavaScript.</a> </p> <p>But there is more... much more.</p> <p>A. require.js - There is a way to load modules of JS code in a very impressive way. These are generally called a module loaders, and are widely considered the future of loading js files since they optimize performance at runtime. yepnope and others exist. Keep this in mind if you are loading more than a few js files. (moved to top by request).</p> <p>B. MVC - There are dozens of Model View Controller frameworks to help you structure code. It is a Pattern, but may be unreasonable for your purposes. You mentioned backbone, knockout and angular... Yes. These can do the trick for you, but I would be concerned that they might be 1) high learning-curve, and 2) overkill for your environment.</p> <p>C. Namespace or Module Pattern. <strong>Are probably the most important for your needs.</strong> To solve global variables just wrap them in a namespace and reference that. These are very good patterns that give rise to module loaders.</p> <p>D. Closure - You mentioned OO JS. On piece of this that is useful is the notion of closures to provide yourself with... private members. At first this is a cryptic idea, but after you recognize the pattern, it is trivial practice.</p> <p>E. Custom Events - It becomes very important to not use hard references between objects. Example: <code>AnotherObject.member;</code> This is because it will tightly couple the two objects together and make both of them inflexible to change. To solve this, trigger and listen to events. In traditional Design Patterns this is the Observer. In JS it is called PubSub.</p> <p>F. The Callback - The callback pattern is what enabled AJAX and it is revolutionizing development in terms of Window 8, Firefox OS, and Node.js - because of something called non-blocking-io. Very important.</p> <p>Do not be afraid. This is the direction to go for long-term and advanced JavaScript implementations.</p> <p>Once you recognize the patterns, it is down hill from there.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload