please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 7 Next
 

7. Objects

Contents:
Object Properties
Creating New Objects with Constructors
Methods
Object Prototypes
Classes in JavaScript
Objects as Associative Arrays
Special Object Methods

Chapter 3, Variables and Data Types, explained that objects are one of the fundamental data types in JavaScript. They are also one of the most important. This chapter describes JavaScript objects in detail. Basic usage of objects, described in the first section below, is straightforward, but as we'll see in later sections, objects have more complex uses and behaviors.

7.1 Object Properties

An object is a data type that contains named pieces of data. Each named datum is called a property. Each property has a name, and the object associates a value with each property name. A property value may be of any type. In effect, the properties of an object are variables within the "name space" created by the object.

Reading and Writing Object Properties

You normally use the . operator to access the value of an object's properties. The value on the left of the . should be a reference to an object (usually just the name of the variable that contains the object reference). The value on the right of the . should be the name of the property. This must be an identifier, not a string or an expression. For example, you refer to the property p in object o with o.p. Or, you refer to the property document in the object parent with parent.document. The . operator is used for both reading and writing object properties. For example:

// Read a property value:
w =index.html image.width;
// Set a property value:
window.location =index.html "http://my.isp.com/my_home_page/index.html";
// Read one property and set it in another property
image.src =index.html parent.frames[1].location

Defining New Object Properties

You can add a new property to an object simply by setting its value. Thus, you might add a property to the object win with code like the following:

win.creator =index.html self;
This line assigns the value of self to the creator property of the object win. If the creator property does not already exist, then it will be created so that the value can be assigned to it.

Undefined Object Properties

If you attempt to read the value of a property that does not exist--i.e., has never had a value assigned to it--you will retrieve the special JavaScript undefined value (which was introduced in Chapter 3, Variables and Data Types).

Once a property has been defined in an object, however, there is no way to undefine it. You may set the value of a property to the special undefined value, by assigning the value of an undefined property, but this just changes the value of the property without actually undefining it. You can demonstrate that the property still exists by using a for/in loop to print out the name of all defined properties:

for (prop in obj) 
    property_list +=index.html prop + "\n";
alert(property_list);


Previous Home Next
Event Handlers Book Index Creating New Objects with Constructors

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

file: /Techref/language/java/script/definitive/ch07_01.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/11/23 18:00,
TOP NEW HELP FIND: 
3.135.247.17: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://techref.massmind.org/Techref/language/java/script/definitive/ch07_01.htm"> [Chapter 7] 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?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .