Javascript Best Practices [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What are some good resources to learn best practices for Javascript? I'm mainly concerned about when something should be an object vs. when it should just be tracked in the DOM. Also I would like to better learn how to organize my code so it's easy to unit test.

Seconding Javascript: The Good Parts and Resig's book Secrets of the Javascript Ninja.
Here are some tips for Javascript:
Don't pollute the global namespace (put all functions into objects/closures)
Take a look at YUI, it's a huge codebase with only 2 global objects: YAHOO and YAHOO_config
Use the Module pattern for singletons (http://yuiblog.com/blog/2007/06/12/module-pattern/)
Make your JS as reusable as possible (jQuery plugins, YUI modules, basic JS objects.) Don't write tons of global functions.
Don't forget to var your variables
Use JSlint : http://www.jslint.com/
If you need to save state, it's probably best to use objects instead of the DOM.

I disagree to the "use a framework" statement to some degree. Too many people use frameworks blindly and have little or no understanding of what's going on behind the curtains.

I liked JavaScript:The Good Parts by Douglas Crockford although it's focused entirely on the language and ignores the DOM altogether.

If you don't feel like reading you can watch this video: JavaScript the good parts by Doug Crockford.

Probably the single most important thing is to use a framework, such as jQuery, or prototype, to iron out the differences between browsers, and also make things easier in general.

YUI Theatre has a bunch of videos (some with transcripts) by Steve Souders, Douglas Crockford, John Resig and others on JavaScript, YUI, website performance and other related topics.
There are also very interested google tech talks on Youtube on jQuery and other frameworks.

You can pick up a lot from Pro JavaScript Techniques, and I'm looking forward to Resig's forthcoming Secrets of the JavaScript Ninja.

As an addendum to the Crockford book, you may also want to check out this piece Code Conventions for the Javascript Programming Language. I also have a slightly different suggestion: instead of using a JS library off the bat, why not create your own? You may write a crappy library (as I did), but you'll learn something in the process. You have existing examples you can use as models. Also, to help give you an understanding of JS design patterns, I shall recommend another book, 'Pro Javascript Design Patterns'.

Related

Typescript vs Marionette.js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I recently discovered Typescript. Coming from an OOP C# background, I find Javascript very difficult to work with and am naturally thrilled about Typescript.
I'm currently using Backbone.Marionette.js in my application to manage future complexity (future because it's still the early stage and not yet that complex).
Now I understand there is still room for Backbone.js to play an important role in my application even if I'm using Typescript, but the more I learn Typescript (and begin to love it), the more I start thinking that its objective in managing complexity in enterprise application overlaps too much with what Marionette tries to accomplish. I understand they do it in two completely different ways but coming from C# I of course prefer the OOP approach instead of yet another JS plugin.
Are my considerations to phase out Marionette out of my application justified or is there less overlap than I think? Keep in mind, I'm new to all 3 Backbone, TypeScript, and Marionette and this is the first application I've used them in, which makes it difficult for me to assess this question.
"TypeScript vs Marionette" is the wrong perspective. That would be like "C# vs WinForms", and asking "Should I stop using WinForms when I switch from C# to VB.NET?" This question doesn't make any sense because it is trying to compare a language to a framework.
There are some things that TypeScript will prevent you from doing with Marionette or any other JavaScript framework that takes advantage of JavaScript being dynamic. But that doesn't mean TypeScript replaces Marionette or any other framework. It only limits the usefulness of the framework due to restrictions that TypeScript places on you.
My answer would be based on the following considerations.
Firstly, what parts of Marionette are you using? If it is just the module loading then you don't need it. If you are using a lot of the templating and eventing it may be useful still. If you aren't sure yet what parts you might use, don't use it until you have a compelling reason to use it.
Secondly, how much can you write before you need to make decision? For example, if you start coding today, how long can you defer the decision. The longer you wait, the more information you'll have to help you make the right choice. If you write plain TypeScript, you can wait until you actually need to bring in a framework or toolkit and then choose the one that solves your real problem.
So really, my answer is start without it and wait until you have a problem to solve before choosing how you solve it.

learning ecma script/javascript directly from the reference [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am trying to learn Javascript.There are some excellent books and great authors that became standard in JS world - As I can see, they all have their own way of interpretation of language. It can be sometimes confusing for the novice in Javascript.I wonder, how usefull can be to learn directly from ECMAScript language specification as it was published by the authors of the language ?
It's probably much better to learn from the tutorials, as the specification is designed for people implementing JavaScript parsers/interpreters, not for people learning JavaScript itself.
If you want to learn from a reference, the MDN is a fantastic resource. There are also plenty of tutorials out there.
The specification is optimized for defining the language from the point of view of its implementors. It is not optimized for teaching it to someone that is new to the language.
A good learning reference has also many things that are not covered in the language spec, like common APIs (like the DOM and a JS framework) and common patterns (ex.: the module pattern, namespaces, etc...). While it is true that some people might have some coding practices you don't agree with you should not immetiately dismiss what they say, unless you really want to learn everything and fall into every trap yourself. As long as you have a mental framework of what you consider to be the best practices in general you should be able to identify what you agree with or not.
JavaScript is one of the most controversial languages in existence, there is no clear author and no clear documentation.
The best project I know of is Mozilla Developer Network (or MDN), it's pretty extensive and comprehensive.

JavaScript design patterns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is a question for general discussion. Are there any good, comprehensive resources for useful JavaScript design patterns. I am trying to avoid references that attempt to coerce JavaScript into, say, Java by imposing patterns more suited to another language. Let's let JS be JS and shape our patterns around the strengths.
Please any discussion would be valued by more than just me, I suspect.
Here's an on-line resource:
Essential JavaScript Design Patterns For Beginners
Here's a good one:
This is an awesome book by O'Reilly. It builds on their "JavaScript: The Good Parts".
It references a lot of the patterns mentioned in the "Gang of Four" book and notes a lot of the problems solved in that book are trivial to solve with JavaScript.
APress has a book called "Pro Javascript Design Patterns" that is probably just what you're looking for. While the author (Diaz Harmes) is certainly not "the gang of four", I think he does a pretty good job; I know I learned quite a bit from it (and it got 4.5 stars on Amazon).
Amazon link: http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X
I'm not sure why no one has listed this, but Addy Osmani's JavaScript design pattern book is fantastic and freely available online. He really digs deep to show you the nuts and bolts of each pattern.
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
This is one of the best design patterns resources I've found for JavaScript
http://shichuan.github.com/javascript-patterns/
It's aimed for JavaScript as we know and not necessarily trying only use concepts of other languages.
Peter Michaux has some decent articles
Also see Crockford's articles (and his book)
A new book on the subject by Stoyan Stefanov: Object-Oriented JavaScript: Create scalable, reusable high-quality JavaScript applications and libraries

Where do I find a good explanation of Javascript-ese [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I realize that title may require explanation.
The language I first learned was C, and it shows in all my programs... even those not written in C. For example, when I first learned F# I wrote my F# programs like C programs. It wasn't until someone explained the pipe operator and mapping with anonymous functions that I started to understand the F#-ese, how to write F# like a F# programmer and not a C programmer.
Now I've written a little javascript, mostly basic stuff using jquery, but I was hoping there was a good resource where I could learn to write javascript programs like a javascript programmer.
Douglas Crockford's - Code Conventions for the JavaScript Programming Language would be a good place to start.
I learned a lot of useful information in regards to code convention through his video tutorials, which I suggest viewing. I posted the link to the first video out of the four in the series.
Also, as suggested by Ben, (which is a book I would also highly recommend) is Douglas Crockford's book JavaScript: The Good Parts
If you want to find good explanations on jQuery, check out the creator, lead developer and fellow StackOverflower John Resig's website/personal blog.
The book Pro Javascript Design Patterns is a great resource:
http://jsdesignpatterns.com/
I really like this article:
A re-introduction to JavaScript
http://eloquentjavascript.net/
It may be a little wordy, but it gives you some excellent examples that will stick with ya.
The more you work with JavaScript/jQuery - especially working with other people's code - the more of the style you will pick up. I recommend reading though the documentation on the jQuery website and maybe picking up a book on Javascript. Then just keep working with it. Also, make sure you keep looking at other people's code - read through the source code of some of the jQuery plugins you are using, and seek out examples online for 'best practices'.
First off, start using JSLint on all your code, for at least a week (http://www.jslint.com). I'm not going to say you should use it constantly, forever and ever, and it's perfect, because it's not. But, it will help you learn a lot about JavaScript, if you take the time to read what it says, and understand why. Even if you disagree, which you may, understand the reasoning behind each message. Then you can decide if it's a tool you'd like to continue using.
The other first step is to pick up a copy of JavaScript: The Good Parts. This is by far the best book on JavaScript. It's short, very easy to read, and really will help you learn better JavaScript. http://oreilly.com/catalog/9780596517748
Other than that, there are a lot of good blogs to follow. You can find these via google, but I'd look at Perfection Kills and John Resig's blog, in particular. I think mine is pretty good too, you can find it via my profile if you're interested :)
I would also recommend "Secrets of a JavaScript Ninja" by John Resig. It will be out in July this year and would definitely be an excellent resource.
Else, Douglas Crockford "JavaScript:The good parts" is excellent.
Javascript has functional programming roots.That makes a big difference compared to the OO world.
Here are some links that helped me jump the fence:
A simple explanation:
http://www.joelonsoftware.com/items/2006/08/01.html
A deeper one:
http://www.hunlock.com/blogs/Functional_Javascript
Read/debug the code of this library:
http://osteele.com/archives/2007/07/functional-javascript
You can read/debug the source code of PURE, a JS templating engine we wrote.The 2nd version was totally rewritten in functional JS.

Core HTML/CSS/Javascript Frameworks [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to improve my web development skills. I'm already familiar enough with HTTP, HTML, CSS, and Javascript, but I'd like to take advantage of some frameworks/helpers for site layout, CSS, and Javascript support.
What are the best HTML/CSS/Javascript web tools/frameworks out there? (In this question, I'm not looking for scripting-language-based frameworks like PHP, Ruby on Rails, or Django - but more "raw" HTML/CSS/Javascript frameworks.)
So far, I've done a quick look at things like YUI, jQuery, Prototype, Scriptaculous, and a few others, but I'm curious to know what people have had good experiences with.
One item per answer would be nice!
I (and many many other people) recommend jQuery.
It's simple and extremely powerful, and has a very large userbase.
JavaScript
According to Google trends (and SO trends), jQuery is the framework of choice.
After using it for a very large project, I do not recommend Dojo, aside from its package management and perhaps its internationalization.
CSS
I recommend Blueprint CSS.
jQuery is the most popular (especially but not exclusively on Stack Overflow), but I don't have much personal experience with it.
Dojo is the second most popular (measuring by search hits on the web for "X javascript" for various framework names as X) and my personal favorite (highly modular, clean "pythonic architecture", rich, always in the vanguard providing browser-independent access to new features such as [[in the recent past]] browser-local storage &c).
I've always really liked Prototype/Scriptaculous despite some involved work with YUI and to a lesser extent jQuery. Perhaps it's because Scriptaculous "gets out of the way" best.
There's a old, but still pretty relevant article at Smashing Magazine on CSS frameworks:
http://www.smashingmagazine.com/2007/09/21/css-frameworks-css-reset-design-from-scratch/
That said I've found Blueprint CSS to be great for HTML wireframes and prototyping, but basically write raw CSS with no framework for every project. Designs are so unique that it really makes sense to write the CSS on a per-project basis.
I tend to start out using reset.css as a baseline and then creating a site-specific screen style sheet.
... as far a css frameworks go, *{padding: 0; margin: 0} works fine, beyond that projects vary a little too much for me.
... +n for jQuery! Does the job just fine with sprinkles of hand rolled js.
IMHO, after a while there comes a point of diminishing returns when trying to rely too much on frameworks. What was originally making life easier can turn into a hammer.
Prototype is still the superior framework if you care about closures and each. JQuery is good for simple projects but when you have to do more than just manipulate DOM elements you're better off with Prototype.
Some good reading
I prefer JQuery, but if you are looking for a really robust UI framework you should check out ExtJS.

Categories