Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

You can see that the function repeat() is typical for a method. As a matter of fact, it is implemented as one. The key statement in this script is:

String.prototype.repeat = repeat

This statement adds a method to all instances of the object (String), although they were already created. The word “prototype” is constant, and its role is to merely specify that the following property or method is a prototype. It may seem at first that repeat() is a method of a nested object. However, prototype is not actually a property but a specification. Here are some of the alert boxes generated by the preceding script segment:


Figures 12-6,7,and 8.  Object enable extendible objects in JavaScript.

It is important to know that object prototypes refer to instances of the object created before the definition of the prototype as well as instances that are created later. The prototype exists only from the moment it is defined. Be sure to refer to such prototype properties and methods only after you have created the prototype. Commenting scripts helps assure that you follow this rule.

Up until now you have seen a method used as a prototype. Obviously, properties can also accomplish this task. In the following script we will look at a property as a prototype of a user-defined object. Here is the script:

function car(make, model, year) {
  this.make = make
  this.model = model
  this.year = year
}

var mine1 = new car("Hyundai", "Lantra", 1996)

car.prototype.wheels = 4 // no 3-wheelers please!

var mine2 = new car("Mazda", "Prodigy", 1996)

if (mine1.wheels == 4 && mine2.wheels == 4) // true!
   document.write("Both cars have ", mine2.wheels, " wheels.")

The message at the end of the script is printed, because all instances of the car object, whether they are created before the definition of the prototype or after it, consist of a property named wheels, holding the value 4, which is assigned to the prototype. It is practically useless to create a data property prototype without assigning it a value, or at least a default one, because you will need to refer to that property later, and such reference automatically creates the property.

Prototypes are probably more useful with built-in objects, because they can modify the object. You can create outstanding methods and properties you never dreamed JavaScript would support. We use prototypes in many examples in this book, especially ones dealing with strings, dates, and arrays. Prototypes were not supported in Netscape Navigator 2.x, but their existence in Navigator 3.0x (and above) turned the JavaScript language upside down. The revolutionary concept is that you can add features to the language itself, almost as if you were one of JavaScript’s developers at Netscape Corp. or Microsoft (although MSIE 3.0x does not support this feature). Note that prototypes can only be added to objects that support instances, as opposed to static objects (such as Math) which do not work with prototypes for obvious reasons.

Summary

In this chapter you learned how to create instances of an object. Instances refer to both user-defined objects as well as to built-in ones. We also discussed constructor functions that build objects or object types. As you will see later in the book, such user-defined objects extend the capabilities of the language and enable structured programming. Lastly, we presented object prototypes—properties of all instances of a certain object type. Although it should be clear by now, do not worry too much about how to use them, as they will be used in many examples later on in the book. Objects are the backbone of JavaScript. Many elements of the language are actually objects, although other languages do not refer to them as such. The classic example for such objects are arrays, discussed later in depth.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us