Your question is quite wide, so I do not think that a complete answer is possible here. But here are a few points.
Regarding the code you provided. You jump on a couple of extra hoops.
- If you are not accessing the DOM in any way, there is no need to wrap the code in
jQuery(document).ready() - There is no need to return an object from a standalone anonymous function unless you close some private functions or data
The object you created can be created more simply (this is good), like this
var Page = { form: {
It’s easier to read and confuse, just do what buys you. see cargo cult planning
Here is an example of using an anonymous self-starting function to create private members
var Obj = (function() { privateFunction( param ) { // do something with param } var privateVar = 10; return { // publicMethod has access to privateFunction and privateVar publicMethod: function() { return privateFunction( privateVar ); } } })();
The structure you used, object literals are very good, as you say, when grouping a set of functions (methods) and properties. This is a kind of namespace. This is also a way to create a Singleton . You can also create many objects of the same class.
JavaScript does not have classes, such as the traditional OO languages (I will get to this), but at the simplest level it’s very simple to create a “template” for creating objects of a certain type. These "patterns" are normal functions called constructors.
// a constructor // it creates a drink with a particular thirst quenchingness function Drink( quenchingness ) { this.quenchingness = quenchingness; } // all drinks created with the Drink constructor get the chill method // which works on their own particular quenchingness Drink.prototype.chill = function() { this.quenchingness *= 2; //twice as thirst quenching } var orange = new Drink( 10 ); var beer = new Drink( 125 ); var i_will_have = ( orange.quenchingness > beer.quenchingness ) ? orange : beer; //beer var beer2 = new Drink( 125 ); beer2.chill(); var i_will_have = ( beer2.quenchingness > beer.quenchingness ) ? beer2 : beer; //beer2 - it been chilled!
There is a lot to know about designers. You will have to search around. There are many examples on SO.
Inheritance, the foundation of OO, is not intuitive in js because it is a prototype. I will not go into this because you most likely will not directly use your own prototype inheritance paradigm. This is because there are libraries that very effectively mimic classical inheritance, Prototype (inheritance) or mootools (class) . There are others there .
Many say that inheritance is overestimated in OO and that you should support composition , and this leads me to what I initially decided to recommend when I started this incoherent answer.
JavaScript design patterns are just as useful as any OO language, and you should familiarize yourself with them.
I recommend that you read JavaScript design patterns . There that he