Summary of value type and reference type in js text description and examples _ javascript tips-js tutorial

Source: Internet
Author: User
The following is an example of the embodiment, usage, and precautions of these two types in JavaScript. 1. Embrace JavaScript

Once well-known JavaScript has doubled its value with the popularity of AJAX. Now JavaScript is no longer just a dispensable auxiliary tool in WEB development, but it even has its own position as a "JavaScript engineer ", even if you are just a WEB Background development programmer, you must understand JavaScript. At least you can see the word "familiar with JavaScript first" in some recruitment requirements. I even want to tell you that you can use JavaScript to develop desktop software, thanks to another development mode of Adobe AIR, that is, using HTML + CSS + JavaScript to develop AIR.

Ii. Value Type and reference type topic
With the intervention of some large object-oriented language-based friends, they tried to use JavaScript to simulate various features of the facial image object, although some of the simulation seems far-fetched, however, we have also seen the strength and flexibility of JavaScript. This article does not discuss JavaScript Object-Oriented Programming Technology for the moment. Let's talk about two types of variables in JavaScript: Value Type and reference type. This often reminds you of "stack ", there are also concepts related to "reference address" or "Pointer". Those who have experience in Java or C # programming are believed to be familiar with these two types. The following is an example of the embodiment, usage, and precautions of these two types in JavaScript.

Iii. JavaScript value types and reference types

(1) Value Type: numerical value, Boolean value, null, undefined.

(2) reference type: object, array, and function.

4. How to understand the value type and reference type and Examples

We can use "chain stores" and "Chain Store keys" to understand it. I don't know if the following metaphor is appropriate. ^-^.

(1) Value Type understanding: the exchange of variables is equivalent to opening a new branch in a new place according to the standard of the chain store (the unified store understands the same variable content, in this way, newly opened stores are irrelevant to other old stores and operate independently.

[Value Type example]

The Code is as follows:


Function chainStore ()
{
Var store1 = 'Li-Ning China ';
Var store2 = store1;
Store1 = 'Li-Ning';
Alert (store2); // Li-Ning China
}
ChainStore ();
// When a value type (or basic type) store2 is passed to another variable (Value assignment), a new memory space is actually allocated, therefore, changing the value of store1 has no effect on store2, because it is not like the reference type. variable exchange actually refers to the address like the same content.


(2) Understanding of the reference type: the exchange of variables is equivalent to copying the key (variable reference address) of the existing store to another boss. At this time, the two bosses manage the same store at the same time, the actions of both shopkeepers may affect the operation of a store.

[Reference Type example]

The Code is as follows:


Function chainStore ()
{
Var store1 = ['Li-Ning China'];
Var store2 = store1;
Alert (store2 [0]); // Li-Ning China
Store1 [0] = 'Li-Ning';
Alert (store2 [0]); // Li-Ning
}
ChainStore ();
// In the code above, store2 only performs a value assignment once. Theoretically, its value has been set. However, it is found that the value of store2 has also changed by rewriting the value of store1, this is the feature of the reference type, which is also worth attention.

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.