Value Type and reference type in Javascript

Source: Internet
Author: User
ArticleDirectory
    • 1. Embrace Javascript
    • Ii. Value Type and reference type topic
    • Iii. Javascript value types and reference types
    • 4. How to understand the value type and reference type and Examples
    • 1. Embrace Javascript
    • Ii. Value Type and reference type topic
    • Iii. Javascript value types and reference types
    • 4. How to understand the value type and reference type and Examples
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 ", maybe you are just a Web Background developer.ProgramPersonnel, you must understand Javascript. At least you can see the word "familiar with JavaScript first" in terms of 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:Numeric value, Boolean value, null, undefined.

(2) reference type:Objects, arrays, and functions.

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) Understanding of value types:Variable exchange is equivalent to opening a new branch in a new place according to the standard of the chain store (the unified store is understood as the same variable content, in this way, newly opened stores are irrelevant to other old stores and operate independently.

[Value Type example]

Function Chainstore ()
{
VaR Store1 = ' Nike China ' ;
VaR Store2 = Store1;
Store1 = ' Nike U. S.. ' ;
Alert (store2 ); // Nike 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 reference types:The variable exchange is equivalent to copying the key (variable reference address) of an 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]

Function Chainstore ()
{
VaR Store1 = [ ' Nike China ' ];
VaR Store2 = Store1;
Alert (store2 [ 0 ]); // Nike China
Store1 [ 0 ] = ' Nike U. S.. ' ;
Alert (store2 [ 0 ]); // Nike U. S..
}
Chainstore ();
// AboveCodeIn, store2 only performs a value assignment once. Theoretically, its value has been set. However, by rewriting the value of store1, it is found that the value of store2 has also changed, which is a feature of the reference type, it is also worth attention.

Reprinted:

Http://www.cnblogs.com/webflash/archive/2009/08/22/1552042.html

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 terms of the 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:Numeric value, Boolean value, null, undefined.

(2) reference type:Objects, arrays, and functions.

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) Understanding of value types:Variable exchange is equivalent to opening a new branch in a new place according to the standard of the chain store (the unified store is understood as the same variable content, in this way, newly opened stores are irrelevant to other old stores and operate independently.

[Value Type example]

Function Chainstore ()
{
VaR Store1 = ' Nike China ' ;
VaR Store2 = Store1;
Store1 = ' Nike U. S.. ' ;
Alert (store2 ); // Nike 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 reference types:The variable exchange is equivalent to copying the key (variable reference address) of an 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]

Function Chainstore ()
{
VaR Store1 = [ ' Nike China ' ];
VaR Store2 = Store1;
Alert (store2 [ 0 ]); // Nike China
Store1 [ 0 ] = ' Nike U. S.. ' ;
Alert (store2 [ 0 ]); // Nike U. S..
}
Chainstore ();
// In the code above, store2 only performs a value assignment once. Theoretically, its value has been fixed. However, by rewriting the value of store1, it is found that the value of store2 has also changed, this is the feature of the reference type, which is also worth attention.

Reprinted:

Http://www.cnblogs.com/webflash/archive/2009/08/22/1552042.html

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.