Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any good examples on how to organize JavaScript code?

I've learned JavaScript and know how to write OO JavaScript, and I can accomplish those assignment like autocomplete, photo gallery with JavaScript, but I just don't know how to organize the codes. For example, when and where to define a function, how to organize those variables, etc.
So I'm looking for some good and simple examples of JavaScript project to learn the good practices. Can you suggest any? (such as a list data filter).

like image 670
wong2 Avatar asked May 27 '11 17:05

wong2


People also ask

What is the best place to put your JavaScript code?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Why should we write a well structured JavaScript code?

Keep Code Organized Our JavaScript code should be organized well so that they can be easily reasoned with. Well organized code don't repeat anything. Functions and classes inside all do one thing only and no more.


1 Answers

http://www.klauskomenda.com/code/javascript-programming-patterns/

That link discusses many different javascript organization patterns, including the one I prefer, the Revealing Module Pattern.

More information about the Revealing Module Pattern is here:

http://www.wait-till-i.com/2007/08/22/again-with-the-module-pattern-reveal-something-to-the-world/

like image 129
AaronShockley Avatar answered Nov 15 '22 13:11

AaronShockley