Object-oriented JavaScript features: References

Source: Internet
Author: User
An important aspect of JavaScript is the reference concept. A reference is a pointer to the actual position of an object. This is an extremely powerful feature. The premise is that the actual object is by no means a reference: the string is always a string, and the array is always an array. However, multiple variables can reference the same object. Javascript is based on this reference mechanism. By maintaining a series of references to other objects, the language provides you with greater flexibility.
In addition, an object can include a series of attributes that can be used to reference other objects (such as strings, numbers, arrays, and so on ). When several variables point to the same object, modifying the underlying object type will reflect all the variables directed to it. Example 2-1: In this example, the two variables point to the same object, but the changes to the object content are global.

Program 2-1. Example of referencing a single object with multiple variables

// Set OBJ to an empty object
VaR OBJ = new object ();

// Objref references other objects now
VaR objref = OBJ;

// Modify attributes of the original object
OBJ. oneproperty = true;

// We can see this change in both variables.
// (Because they reference the same object)
Alert (obj. oneproperty === objref. oneproperty );

I have previously mentioned that self-modified objects are rare in JavaScript. Let's look at an instance in this situation. The array object can use the push method to add additional items to it. Because at the core of the array object, the value is stored as the property of the object, and the result is similar to the case in 2-1 of the program, an object is changed globally (as a result, the values of multiple variables are changed at the same time ). See program 2-2.

Program 2-2. Example of Self-modified object

// Create an array of projects
VaR items = new array ("one", "two", "three ");

// Create a reference to the project Array
VaR itemsref = items;

// Add an item to the original array
Items. Push ("four ");

// The length of the two arrays should be the same,
// Because they all point to the same Array object
Alert (items. Length = itemsref. Length );

It is important to remember that a reference always points only to the object to be referenced, rather than the reference itself. For example, in Perl, a reference may point to another variable that is also referenced. But in Javascript, it traces down the reference chain until it points to the core object. The program 2-3 demonstrates that the physical target has changed and the reference still points to the original object.

Program 2-3. Changing the reference of an object while maintaining integrity (see #9 oerrite reply)

// Set items as an array (object) of a string)
VaR items = new array ("one", "two", "three ");

// Set itemsref to reference items
VaR itemsref = items;

// Point items to a new object
Items = new array ("new", "array ");

// Items and itemsref now point to different objects
// Items points to new array ("new", "array ")
// Itemsref points to new array ("one", "two", "three ")
Alert (items! = Itemsref );

Finally, let's look at a strange example. It seems to be a self-modified object, but it acts on a new unreferenced object. When String concatenation is executed, the result is always a New String object, instead of the version after the original string is changed. This can be seen in program 2-4.

Program 2-4. Example of object modification acting on a new object instead of a self-modified object

// Make item equal to a New String object
VaR item = "test ";

// Itemref also references the same string object
VaR itemref = item;

// Concatenate a new object on the string object
// Note: This creates a new object without modifying the initial object.
Item + = "ing ";

// The values of item and itemref are not equal because
// A new object is created
Alert (item! = Itemref );

If you have just touched on it, referencing may be a big topic. However, understanding how references work is extremely important for well-written and clean JavaScript code. In the following sections, we will explore several new and exciting features that are also important for writing good and clean code.

Trackback: http://www.cnblogs.com/zitiger/archive/2007/06/08/776750.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.