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.
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.
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
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;
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);
file: /Techref/language/JAVA/SCRIPT/definitive/ch07_01.htm, 7KB, , updated: 2019/10/14 15:00, local time: 2024/12/1 18:45,
3.141.29.90: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? <A HREF="http://techref.massmind.org/Techref/language/JAVA/SCRIPT/definitive/ch07_01.htm"> [Chapter 7] Objects</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to techref.massmind.org! |
.