tags:

views:

35

answers:

1

i want to make an object which can do all the actions my form needs -- it can validate the form, submit the form, and so forth. i want javascript to do all these things. i no longer want to write same code several times over in procedural fashion javascript in global namespace. i want to create Javascript object to handle these functions of the form and i want to have everything live in a namespace to avoid conflicts.

i have hard time to understand how javascript OO works compared to other language with classical inheritance vs prototype inheritance. can somebody show me how to create a class, and then instantiate 2 objects of that class and bind each of them to two identical form on the same page?

i look at module pattern but it seems for singleton only.

thanks if anybody can help

+2  A: 

Don't reinvent the wheel. If you want to perform form validation and submit based on validation you can try using jQuery and jQuery validation.

To understand Javascript's prototype-based OO model, there are many resources:

  1. Javascript: The world's most misunderstood programming language (start here - it's by Douglas Crockford)
  2. Introduction to Object Oriented Programming in Javascript (from the Mozilla Developer Center)
Vivin Paliath
thank you for the links, the mozilla one looks better than most tutorials i see so far. unfortunately most show only simple examples but i dont see how to go from global namespace to namespaced classes. i will continue reading the sources u provide before coming back with more questions -- maybe it will become clear
JCm
You can have a form of namespacing through the module pattern. Of course, this means that you have only a singleton. In most cases that should be what you need. Otherwise you can resort to using object literals. Another way of namespacing is to have `var my = {}; var my.namespace = {}; var my.namespace.myFunc = function() { ... };` IMO the module pattern is better because it provides encapsulation. Be careful though. Namespacing and making objects are two different things.
Vivin Paliath