Electric Type

Multimedia

About Us

News

Help

Advanced JavaScript Tutorial
Lesson 4

by Thau!

Page 5 — Object Advantage

We've seen plenty of examples of JavaScript's built-in objects and how they're used: Window, Image, Document, Form, and a bevy of other objects. Remember: An object is just a special kind of data, made up of properties and methods. Properties are the things that an object knows about itself, while methods are the things that the object knows how to do.

For example, the Window object in JavaScript has properties, like window.status, which can be read and set. By setting the status property of the Window object to a string, it makes that string appear in the window's status bar. Window also has methods, like window.focus(), which brings a window forward. If this stuff is unfamiliar to you, you should probably read Part I, Day 5 for more details.

Objects provide a great way to organize information. First, we deal with real-life objects all the time. A monkey, for example, is an object. Its properties include height, weight, furriness, and sassiness. All monkeys have the same set of properties. It's the values of those properties that makes one monkey different from another. Monkeys also have methods, or things they do, like play(), eat(), sleep().

Objects are also handy because they allow you to preserve the "sense" of words. Take the notion of focus() in JavaScript, for example. Focus(), appropriately enough, brings things into focus. When focus() is applied to a window, it brings the window forward. Blur does the reverse — try it.

open little_window

Click on the link below to see how focus brings the little window forward.
little_window.focus();

However, when you apply focus() to a text box, it puts a typing cursor inside the box.

window.document.the_form.the_first_text.focus();
window.document.the_form.the_second_text.focus();

Spooky, huh? JavaScript knows that focus does one thing for a window and something else for a textbox. The idea of focus is the same in both cases, so it makes sense to call it the same thing. But it also makes sense that focus() does slightly different things for two different objects. This notion of having one method name do different things for different objects is called "polymorphism," and it's fundamental to object-oriented programming.

Just as you can have the same method do different things for different objects, you can also have the same property mean different things in different objects. The length property, for example, works for both strings and arrays. The string.length returns the length of a string, while array.length returns the number of things in the array. Both properties are called length, but they mean slightly different things in the context of different objects.

Objects also make it easy to cut and paste code together. If you have a good library of objects, you can just copy an entire object and plunk it into whatever script you need. This is a lot harder when you don't have your code organized into clear objects.

Since objects are so handy, JavaScript gives you the ability to create your own.

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.