Value types and reference types in JS summary text description and example _javascript skills

Source: Internet
Author: User
One, embrace JavaScript

The once-well-known JavaScript has multiplied with the popularity of Ajax, and now JavaScript is no longer just a dispensable helper in web development, and even has a "JavaScript engineer" dedicated to its position, If you're just a web developer, you have to understand JavaScript, and at least you can see the word "familiar with JavaScript first" in some of the relevant job requirements. Even I have to tell you that you will be able to develop desktop software with JavaScript, thanks to another development model of Adobe AIR, which uses html+css+javascript to develop air.

second, value type and reference type topic
As some of the major object-oriented language base friends intervene, they try to use JavaScript to simulate the various features of the surface objects, although some of the simulation is far-fetched, but also let us see the power and flexibility of JavaScript. JavaScript object-oriented Programming technology is not discussed in this paper. Just talk about two types of variables in javascript: that is, value types and reference types, which usually remind you of "stacks," plus "reference address" or "pointer" concepts, and people with experience in Java or C # programming believe that these two types are not unfamiliar. Here are some examples of how these two types of JavaScript are embodied, used, and noted.

What are the JavaScript value types and reference types

(1) value type: Numeric, Boolean, null, undefined.

(2) Reference type: object, array, function.

Iv. How to understand value types and reference types and examples

We can use "chain stores" and "Chain Keys" to understand, do not know the following analogy is not appropriate, ^-^.

(1) Value type Understanding: The exchange of variables is equal to a new place in accordance with the standard of the chain store (Unified store to understand the same variable content) a new branch, so that the newly opened shop and other old stores are not related to each other and operate.

"Value type Example"
Copy Code code as follows:

function Chainstore ()
{
var store1= ' Nike ';
var store2=store1;
Store1= ' Nike U.S.A. '
alert (Store2); Nike
}
Chainstore ();
When you pass a value type (which can also be called a base type) Store2 to another variable (Assignment), in fact, a new memory space is allocated, so changing the value of Store1 has no effect on Store2 because it is not like a reference type, and the exchange of variables is actually an exchange of addresses that refer to the same content.

(2) Reference type Understanding: The exchange of variables equals the key of the existing store (variable reference address) copy one to the other boss, when two bosses manage a shop at the same time, and the behavior of two bosses can have an impact on the operation of a store.

"Reference type Example"
Copy Code code as follows:

function Chainstore ()
{
var store1=[' Nike '];
Var store2=store1;
Alert (store2[0]);//nike the store1[0]= ' Nike U.S.A. '
Alert (store2[0]);//nike U.S.A.
}
Chainstore ();
//In the above code, Store2 only one assignment, theoretically its value has been fixed, but later by rewriting the value of Store1, found that the value of Store2 also changed, which is the characteristics of the reference type, but also we should pay attention to the place.

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.