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.