HTML and CSS Reference
In-Depth Information
this.setName(name);
this.setType(type);
/**
* Gets the pet's name
*/
this.getName = function(){
return _name;
}
/**
* Sets the pet's name
*/
this.setName = function(name){
_name = name;
}
/**
* Gets the pet's type
*/
this.getType = function(){
return type;
}
/**
* Sets the pet's type
*/
this.setType = function(type){
_type = type;
}
}
As you can see, the pet model follows the exact same structure as the user
model. To use these together, you can do the following.
var sue = new user('Suzanne', 'password', null); // First create a new user with
no pet
var jack = new pet('Jack', 'dog'); // Create a new pet
sue.setPet(jack); // Assign the new pet to the user
/**
* By calling getPet, you now have access to all of the pet's methods and
* attributes from the user
*/
alert(sue.getName() + 'has a favorite ' + sue.getPet().getType() + ' called ' +
sue.getPet().getName());
 
Search WWH ::




Custom Search