HTML and CSS Reference
In-Depth Information
/**
* Sets the user's name
*/
this.setName = function(name){
_name = name;
}
/**
* Sets the user's password and encrypts it before assignment
*/
this.setPassword = function(password){
_password = password.encrypt(); // .encrypt() doesn't really exist!
}
/**
* Returns the user's favorite pet
*/
this.getPet = function(){
return _pet;
}
/**
* Sets the user's favorite pet
*/
this.setPet = function(pet){
_pet = pet;
}
}
As you can see, there's a significant amount of code here to achieve what feels
like very little. However, the idea is that you can use this new user model
anywhere in your application, and no matter what data you provide to it, it will
act in a predictable manner throughout your application.
The beauty of using models is that you can create relationships between them.
For instance, using the previous example, every user has a pet, but wouldn't it
be nice to find a way to describe that pet? To do this, you can create a pet
model.
You could just as easily add pet attributes to the user model, but if you need to
describe the pet in more detail in the future, you end up with a cluttered user
model. Having a separate model allows you to create new pet attributes in the
future, without damaging the integrity of your application. The pet model is as
follows .
var pet = function pet(name, type){
var _name = null,
_type = null;
 
Search WWH ::




Custom Search