spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 6

[next]

On-Line Marketing Specialist NEEDED!
Aquent
US-VA-Reston

Justtechjobs.com Post A Job | Post A Resume
Developer News
Cisco Lawsuit: A Test for the GPL?
Shifts for Enterprise Linux, Green Networks in '09
Gifts for All in Linux 2.6.28

The JavaScript Diaries: Part 6

  1. Introduction
  2. Data Types & Variables
  3. Operators
  4. Functions
  5. Conditional Statements and Loops
  6. Objects
  7. Browser-Based Objects
  8. Window Methods
  9. Window Event Handlers
  10. Navigator, Screen, History and Location Objects
  11. Arrays: Part 1 - Introduction
  12. Arrays: Part 2 - Multiple Array Types
  13. Arrays: Part 3 - Array Properties and Methods
  14. The Math Object
  15. The Date Object

By 

This week we are going to go a little deeper into our study, a process that will last for several weeks. I want to make sure you fully grasp this subject as it's a very important topic, but it can be challenging to understand in the beginning. Take your time, read carefully, and contemplate what you've learned. Eventually, you'll gain an understanding.

JavaScript Objects

As I mentioned in the first installment, JavaScript is an object-based scripting language. This means that the JavaScript interpreter sees the existing data structure as objects rather than a bunch of scattered, random items. As defined by Webopedia an object is "generally, any item that can be individually selected and manipulated.... In object-oriented programming, for example, an object is a self-contained entity that consists of both data and procedures to manipulate the data." Objects can refer to HTML elements, certain parts of the browser, and often refer to custom-made objects. For the most part, the code that is written is used to control objects.

In an object, related properties and methods are grouped into a single package, hence the term "object." Like a function, an object allows you to organize related items within a script. In fact, a function is an object and can be thought of as a 'thing.' A good example of this is a car, which is an object and a thing. An example of an object is:

  function getObject(param1,param2,param3) {
    this.param1=param1;
    this.param2=param2;
    this.param3=param3;
  }

Object Properties

An object has properties. A property is a component or part of the object. Using the above example, a car's properties may include a radio and tires. Properties are data containers, just like variables. Besides being a part of an object, they also give information about the object. One important difference is that properties give information about the window object whereas variables do not. An object holds properties that can be accessed from the outside for use in the overall script.

The properties of an object are accessed through the use of a dot operator. The format would be the name of the object, followed by the dot operator, followed by the property you are trying to access: car.radio and car.tires. You can even go to deeper and obtain further properties, such as: car.cdplayer.speakers.wires.

Object Methods

An object can also have methods. A method does something to the object or with the object, just like the car, which can go forward and backward. A method may cause a new browser window to open, or it may cause text to be selected. Methods use parentheses just like functions, i.e. mainObject.getPrice(). A main difference, however, is that methods can act on a window object whereas functions cannot. According to Core JavaScript Guide 1.5: "A method is a function associated with an object. You define a method the same way you define a standard function."

Review

Let's review what we've just covered. An object is a thing, a collection of properties grouped together. Properties are individual parts of the object. A method is something that causes the object to do something. It may help to think of objects and properties as nouns and methods as verbs.

Now, let's take a look at objects in greater detail. First, we'll look at how to create your own. Later, we'll look at the ones that are built into the JavaScript language.

home / programming / javascript / diaries / 6

[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
An Introduction to 3D · Email Marketing Terms to Know · Search Engines 101: Paid Vs. Natural Search
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Mastering SSH: Connecting, Executing Remote Commands and Using Authorized Keys · Connecticut Town Lays Groundwork for Merged School, Municipal VoIP Network · Wi-Fi for your Car, Truck, or MPV

Created: July 1, 2005

URL: