HTML and CSS Reference
In-Depth Information
The Model
The M in MVC stands for Model. A model is the part of the application that
dictates how different types of data are handled. A model is simply a JavaScript
object that can represent an entity of some kind. For instance, you might create
a model for a user within your application, like so:
var user = function user(){}
TIP: You will notice that I have named the function, as well as
assigned it to a variable. This essentially creates a named function
instead of an anonymous one. This is useful in many instances, such
as debugging, as you can see the method name in a stack trace.
A user will typically have attributes, such as name, password, and pet. It's best
to create these attributes by using instance variables (variables that are only
accessible from within the object/model) and then creating privileged getters
and setters to modify or retrieve those values. This allows you to then create
rules around those attributes. For instance, you can have a setter for the
password that accepts a plain text password and then encrypts its value within
the object. By omitting a getter, you can also prevent the user's password from
being retrieved from the user object by another piece of code. The next code
example shows the evolution of the user model.
var user = function user(name, password, pet){
var _name = null,
_password = null,
_pet = null,
_self = this;
name = null;
password = null;
pet = null;
* Returns the user's name
this.getName = function(){
return _name;
Search WWH ::

Custom Search