Wednesday, August 6, 2008

Objects

An object is a "package" of data; a collection of properties (variables) and methods (functions) all classed under a single name. For example, imagine that there was an object named car. We could say that the car object possesses several properties: make, model, year, and color, for example. We might even say that car possesses some methods: go(), stop(), and reverse(). Although car is obviously fictional, you can see that its properties and methods all relate to a common theme.

In JavaScript you may create your own objects for storing data. More commonly, though, you will use the many "built-in" objects which allow you to work with, manipulate, and access the Web page and Web browser. This set of pre-existing objects is known as the "Document Object Model".

Document Object Model
Often referred to as the DOM, this object model is a hierarchy of all objects "built in" to JavaScript. Most of these objects are directly related to characteristics of the Web page or browser. The reason we qualify the term "built in" is because the DOM is technically separate from JavaScript itself. That is, the JavaScript language specification, standardized by the ECMA, does not actually specify the nature or specifics of the DOM. Consequently, Netscape and Microsoft have developed their own individual DOM's which are not entirely compatible. Additionally, the DOM stands apart from JavaScript because it could theoretically be accessed by other scripting languages as well.

In summary, then, what we refer to as "JavaScript" is actually made up of both JavaScript, the language, and the DOM, or object model which JavaScript can access. In a future WDVL article we will take a closer look at the DOM and its current and future role.

Below is a graphical chart illustrating a high-level view of Netscape's DOM. Microsoft's DOM is actually a superset of Netscape's, and so the chart below actually represents a subset of Microsoft's own DOM.


Reprinted from Netscape's JavaScript Guide



Properties
Access the properties of an object with a simple notation: objectName.propertyName. Both the object name and property name are case sensitive, so watch your typing. Because a property is essentially a variable, you can create new properties by simply assigning it a value. Assuming, for instance, that carObj already exists (we'll learn to create a new object shortly), you can give it properties named make, model, and year as follows:


carObj.make="Toyota";
carObj.model="Camry";
carObj.year=1990;
document.write(carObj.year);


A JavaScript object, basically, is an array. If you're familiar with other languages you probably recognize an array as a collection of values residing within a single named data structure. You can access an object's properties either using the objectName.propertyName syntax illustrated above, or by using an array syntax:


carObj["make"]="Toyota";
carObj["model"]="Camry";
document.write(carObj["year"]);


Methods
Unlike a basic data array, an object can also contain functions, which are known as methods when part of an object. You call a method using the basic syntax: objectName.methodName(). Any arguments required for the method are passed between the parentheses, just like a normal function call.

For example, the window object possesses a method named close(), which simply closes the specified browser window:


window.close();


Creating Objects
Most of the time you will be referencing objects which are built-in to the DOM. However, you may want to create your own objects for storing data within a JavaScript program. There are several ways to create a new object, but we'll look at two: creating a direct instance of an object and creating an object prototype.

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);

0 comments:

Search

My Blog List

  • ndevil.com – Samsung Demo at IFA 2011 of the Samsung Galaxy Tab 7.7 Tablet.
  • This lesson introduced the techniques that will enable you to create applications that can run tasks in the background. It discussed the Asynchronous Pat...
  • Hi, Join the world recognized MBBS program in Ukraine. You can now earn your MBBS degree at an expense less than 30% by doing it in one of th...
  • INTRA NIFTY *f**u**t**u**r**e*S FOR 29.10.09 Hi Friends, Happy to meet you all again. I am going to post my 98% accuracy Nifty *f**u**t**u**r**e*s Intra...
  • Do It Yourself *P**r**o**j**e**c**t* : Menyelenggarakan Multipoint Video Conferencing Menggunakan Skype Gubernur Sulawesi Selatan Berkomunikasi dengan T...
  • A: There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor is a code which is responsible for creating an instance o...
  • online internet,online internet business,free online internet,online internet speed,online internet game,online internet jobs,online internet sales,free on...
  • A table scans
  • COMMUNAL DEADLOCK AND A WAY TO SOLVE IT _________________________________________________ Address delivered at the Session of the All India Scheduled Caste...
  • ** The application server is run by the Computer Administration Group at the Mechanical Engineering Departmentt. One of the objectives of this course is ...
  • Knowledge Sharing Forum - Discuss programming, technology, movies, music, video Advanced EJB Interview Questions What is Web Hosting ? How to earn money on...
  • इंडिक ट्रांसलिटरेशन गूगल ट्रांसलिटरेशन इंग्लिश शब्दों को हिन्दी में टाइप करने में मदद करता है। इसकी खासियत है के आप कोई भी हिन्दी शब्द का इंग्लिश स्पेलिंग ...
  • - Q: Can you retrieve a test case once you have deleted them in Test Director? Q: How to use TestDirect like a Dashboard? Q: After creating ...
  • At Blog.co.in, we have a mixed blend of bloggers. Some are full time bloggers who like to earn money. And on the otherhand, a large number of users who ...
  • Demo Requirements This session requires 1 machine (called Server1 in this document). * * Minimum Hardware Requirements: Server1 *Processor* 400Mhz ...
  • IE App Compat VHD - Time bomb 4/2009 Posted: 02 Jan 2009 01:18 PM CST VPC Hard Disk Image for testing websites with different IE versions on Windows XP ...
  • *Adding DCOM to the Simple Client* To activate our DCOM server we take the basic COM client shell and add some additional methods to make it DCOM-ready....
  • EMCO *M**S**I* Package Builder Enterprise 4.0.4 EMCO *M**S**I* Package Builder Enterprise 4.0.4 - Monitor any system changes using drivers. Monitor and ...
  • BBC World Business News Podcast is retty awesome. It covers international news headlines and US economic headlines as well, and is unique in its addition o...
  • - FILE SPACING: # double space a file sed G # double space a file which already has blank lines in it. Output file # should contain no more than o...
  • Merge Modules are a feature of Windows Installer that provides a standard method for delivering components, ensuring that the correct version of a compon...
  • Before we begin with main program let us now see how to attach a menu to a blank form. Carry out the following steps: 1. Drag in the 'MainMenu...
  • * Introduction * When you use managed C++, your code is handled by the common language runtime (CLR). That means things like garbage collection and in...
  • By default, ActiveX controls do not support tooltips. This article demonstrates how to add tooltip to an ActiveX control. This control can be used to ad...
  • One way to reuse a component within another component is using *containment*. Another way to do so is using *aggregation*. In *containment* the outer com...