I’m creating a javascript object, and I want to know the best practice in doing so (for my needs).
What I want to do is create an Email namespace, which has some functionality built in, but also the ability to create an instance of an Email.
What I want to have:
- The ability to call Email.Validate(string)
- The ability to create an instance of an Email object that fits my business needs (has the following properties):
- A database id (undefined if this is new)
- The email address
- A person’s name (to associate with the address)
- etc . . .
Now what I have laid out is just an idea, so maybe someone has a better design pattern to use. But I’m looking for an idea of how to have functionality for an email the isn’t unique to a specific email instance (validation, etc), and also the ability to have an email object which can use this functionality, with a focus on a good structure/design.
I hope this makes sense, but if not please ask for clarification.
This ought to do it:
Use it like so:
Demo: http://jsfiddle.net/mattball/sfE35/