Example Of Creating Objects | ||
| ||
| taken from http://www.wdvl.com/Authoring/JavaScript/Tutorial/objects.html direct instance of an object Despite the awkward sound name, a "direct instance of an object" simply means creating a new single object, such as myPetDog: myPetDog=new Object(); myPetDog.name="Barney"; myPetDog.breed="beagle"; myPetDog.year=1981; Assigning a method to your new object is also simple. Assume that you already have coded a function named woof(), which causes a barking sound to play: myPetDog.woof=woof; prototype of an object Sometimes, you'll want to create a "template" or prototype of an object. This does not create an actual instance of the object, but defines the structure of the object. In the future, then, you can quickly stamp out a particular instance of the object. Suppose that instead of myPetDog, you created a prototype object named petDog. This object could then be a template for a particular pet dog object. First, create a function which defines the petDog structure: function petDog(name, breed, year) { this.name = name; this.breed = breed; this.year = year; } Now that the petDog prototype has been set, you can quickly create single instances of a new object based on the petDog structure: myPetDog=new petDog("barney","beagle",1981); yourPetDog=new petDog("max","terrier",1990); | ||
| | ||
| |