-1

I've been writing Javascript code and using extensively the jQuery library. My code is not very big although for me is a mesh right now. I would to know if there is a jQuery best practices related to the way you write the code and how to structure it, not only related to performance.

I am asking this because from my point of view using jQuery configures a different way of writing code and I think that people who are used to writing a lot of jQuery + Javascript code have methods to organise the code.

I also would like to know if there is a "common & good" way of documenting jQuery code just like Javadoc.

Thanks in advance.

Jonás
  • 1,459
  • 4
  • 27
  • 43

4 Answers4

2

The question is extremely difficult to answer because it is so broad. If you want a guide to style your code look here.

http://javascript.crockford.com/code.html

As for how to structure it, it really depends on the framework that you are using. Each one has a different style. jQuery plugins and modules have a specific structure. http://docs.jquery.com/Plugins/Authoring . Backbone or Ember JS are structured differently.

As for documentation. JSDoc works great. Here is a link to the doc creation tool. https://github.com/jsdoc3/jsdoc . You can find other documentation online.

Taylor Hakes
  • 606
  • 7
  • 12
1

It sounds like you are ready to move on to something like backbone.js. Backbone is a framework that can help you write more structured code.

I like this introduction: https://github.com/kjbekkelund/writings/blob/master/published/understanding-backbone.md/

geon
  • 8,128
  • 3
  • 34
  • 41
0

It sounds like you are looking for something to split code out in to modules? requirejs allows you to split your code in to separate files, which can make it much easier to reason about.

Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
0

I'm also new to jQuery(just start a week ago :) ). I have been asked to write a web-front completely based on jQuery and HTML.

In my opinion, if I'm using this jQuery stuff, I will write as close as to it. I mean, anything in javascript which could be replaced by jQuery's functions and methods and whatever, I will replace it. like getelementid will be replaced by the $("id"). It do makes the code much more simple, in fact.

I used to write C++ and Java code. So I may have some intent to use the style when I'm coding with those language. That is separate every class, objects, functions into different file. The interface and the execution code will never in one file, and load them in the head of HTML. I do know not everyone, especially pro guys may not using this way to write the code, but I feel much organized this way. Also, it is easy to debug and make change.

Arthur0902
  • 209
  • 3
  • 14