HTML and CSS Reference
In-Depth Information
through the pages. The interesting parts of this code are the function declarations them-
selves. For example, when you look at the code for the lipTo function, you might think that
the function is called FlipToAPage because that's what was declared. However, this isn't the
case. The methods are called using the alias property that assigned the function. When using
the code, the runtime knows that it's a method, not a property, and it expects the method to
be called with parentheses:
//This line throws an exception because the object does not support this method
//This line works because this is what the method has been named.
Creating objects inline as the book object is in the previous code sample is useful only
when it is used in the page where it's defined, and perhaps only a few times. However, if you
plan to use an object often, consider creating a prototype for it so that you can construct
one whenever you need it. A prototype provides a definition of the object so that you can
construct the object using the new keyword. When an object can be constructed, such as with
the new keyword, the constructor can take parameters to initialize the state of the object, and
the object itself can internally take extra steps as needed to initialize itself. The following code
creates a prototype for the book object:
function Book() {
this.ISBN = "55555555";
this.Length = 560;
this.genre= "programming";
this.covering = "soft"; = "John Doe";
this.currentPage = 5,
this.flipTo = function FlipToAPage(pNum) {
this.currentPage = pNum;
this.turnPageForward = function turnForward() {
this.turnPageBackward = function turnBackward() {
var books = new Array(new Book(), new Book(), new Book());
JavaScript consists of objects. Everything in JavaScript is an object. Each object is based on
a prototype. Whenever you create a new instance of an object, that instance is based on
the object's prototype.
Search WWH ::

Custom Search