HTML and CSS Reference
In-Depth Information
empty array. Rather than assigning the pets array passed to the method to the
_pets array in the pets object, you iterate through every pet in the new array and
call the addPet method. The reason for this is to ensure that any new pets still
pass through the same code used to add a pet to the user object, which may
contain validation or modify each pet object. To make use of the new code, you
can do something similar to the following JavaScript code.
var user = new user('Suzanne', 'password');
var pet1 = new pet('Jack', 'dog');
var pet2 = new pet('Snoop', 'dog');
user.appPet(pet1);
user.addPet(pet2);
var message = user.name + ' has ' + user.getPets().length + ' pets. ' +
user.name + ' has';
for(var i = 0; i < user.getPets().length; i++){
message += ' a ' user.getPet(i).getType() + ' called ' +
user.getPet(i).getName();
}
alert(message);
This should output something similar to ''Suzanne has 2 pets. She has a dog
called Jack a dog called Snoop''.
It's important to remember that your models are simply JavaScript objects, so
you can add any methods to manipulate the variables within them or output
things in a certain way.
There aren't that many models within MoMemo. The best way to explain how
the models work with each other is through a class diagram.
Although JavaScript is a classless language, you can still use the class analogy
to describe how an object is formed through creating a constructor, methods,
and instance variables within that object.
A class diagram shows what methods and properties a class will have, and how
they interact with other classes.
NOTE: To keep things as simple as possible in this topic, I will only
cover how to read basic UML class diagrams including properties,
methods, and common associations. If you'd like to learn more about
UML and the different diagrams available, feel free to check out
www.agilemodeling.com/essays/umlDiagrams.htm .
 
Search WWH ::




Custom Search