please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 3
Variables and Data Types
Next
 

3.6 Objects

An object is a collection of named pieces of data. These named pieces of data are usually referred to as properties of the object. (Sometimes they are called "fields" of the object, but this usage can be confusing.) To refer to a property of an object, we refer to the object, and follow this reference with a period, and follow the period with the name of the property. For example, if an object named image has properties named width and height, we can refer to those properties like this:

image.width
image.height
Properties of objects are, in many ways, just like JavaScript variables and can contain any type of data, including arrays, functions, and other objects. Thus, you might see JavaScript code like this:

document.myform.button
which refers to the button property of an object which is itself stored in the myform property of an object named document.

As mentioned above, when a function value is stored in a property of an object, that function is often called a method, and the property name becomes the method name. To invoke a method of an object, use the . syntax to extract the function value from the object, and then use the () syntax to invoke that function. To invoke the write() method of the document object, we use code like this:

document.write("this is a test");

Objects in JavaScript have the ability to serve as associative arrays--that is, they can associate arbitrary data values with arbitrary strings. When objects are used in this way, a different syntax is generally required to access the object's properties: a string containing the name of the desired property is enclosed within square brackets. Using this syntax we could access the properties of the image object mentioned above with code like this:

image["width"]
image["height"]
Associative arrays are a powerful data type, and are useful for a number of programming techniques. We'll learn more about objects in their traditional and associative array usages in Chapter 7, Objects.


Previous Home Next
Functions Book Index Arrays

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch03_06.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/12/26 22:35,
TOP NEW HELP FIND: 
3.133.120.64:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/language/java/script/definitive/ch03_06.htm"> [Chapter 3] 3.6 Objects</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?