Simple analysis of basic types and reference types for JavaScript

Source: Internet
Author: User

Because of the use of ExtJS as a front-end framework in recent projects, it is not necessary to use JavaScript, so I recently studied JavaScript on my own to facilitate the development of the smooth. But in real-world development, I found that JavaScript is a very simple language to get started with, but when it's really used, we'll find that he is a weak type of language, and from a certain point of view it may be a feature that is both interesting and powerful, but also prone to problems.

The landlord himself is also in the study of the language, so write a blog is to consolidate their knowledge, but also to share with you I learn some of the things we have used but not necessarily care about the small details of a collation analysis, if there is wrong place, or hope that we propose to give amendments, common growth.

Well, gossip doesn't say much, we rush into the basics of typing and the reference type of explanation.

First of all, I want to give you an analogy, everyone has an identity card, like this white rich beautiful identity card.

if mating to do things, some may ask mating to ID card copy, some may ask mating to ID card original, here the copy white Rich beauty can do to everyone printed a copy, who asked mating to mating can copy a copy to whom, This will not affect each other for the use of this copy, but the original identity of different, he will always be in the hands of mating, will not be in your hands, who will only and white rich beauty to contact, and by mating to him, if there is another person to use also to pass white rich beauty. and contact white rich beauty need to call, so this call is equivalent to a pointer, he will always point to mating (unless mating is enough to change the number), and the identity card is mating this object is a property value, and you I call him the person is the reference type, our phone book is always stored a point to the mating of the phone To get his object, but as a reference type, we cannot hold his identity card like mating, but only hold his phone number, which is a reference to an object.


It seems that there is a kind of mdzz feeling, and the landlord said too long not to see?

Here we look at an example, of course, if you can read, you can directly say a landlord mdzz.

    var a=1;    var b=a;    alert (b);//What do you think B equals?    a = 3;//What do you think I'm going to do?    alert (b);//Do you know how much B is at this time? </span>
Do you understand?!!! Whether you see it or not, we continue to look at another question.

                var mother = new Object ();                Mother.name = ' Itnao ';                var son1 = mother;                var son2 = mother;                alert (son1.name);                alert (son2.name);                Mother.name = ' Tomommi ';                alert (son1.name);//guess what can be shown here                alert (son2.name);//ibid.                Delete mother.name;//guess what type can use this method                alert ( Son1.name);//guess what results to output

Well, my question has been raised, and then we need to answer the question.

I. Basic Types

Concept: Basic type contains (Undefined,null,num,string,boolean) the basic type of access is accessed by value, that is, I can manipulate the actual value saved to the variable.

1. Basic type is present in stack memory

Let's start by creating a few variables.

     var a=1;     var b=2;
Let's take a look at how they're stored in stack memory.

As you can see here, in the stack memory, the identifier of the variable and the actual value of the variable are stored.

2. The value of the base type cannot be changed

Or look at the code first.

      var name = "Itnao";      name.tolowercase ();//lowercase Operation      alert (name);//itnao
In fact, the last name is still equal to "Itnao", and the Calling. toLowerCase ()method returns a new string, and does not change the value in name.

We also cannot add properties and methods to the values of the base type

      var name= "Itnao";         name.age=21;     alert (name.age);//undefined
3. The basic type of comparison is worth comparing, this is different from the reference type of

    var a = "a";    var B = "a";    Alert (A = = B);//true
Because the value of the base type is the real value, when compared, it is actually compared to the value of the stack memory, but the reference type is very different, specifically how we proceed to look down.

two. Reference Types

Concept: JavaScript, in addition to the basic type (Undefined,null,num,string,boolean), can be called a reference type, and the value of the reference type is the object that is stored in memory. Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly. When manipulating an object, it is actually a reference to an object, not an actual object, and for that reason the value of the reference type is passed by reference. Of course, this is not very rigorous, because when copying a variable that holds an object, the object's reference is manipulated. But when you add a property to an object, the action is actually an object.

1. Reference types are stored in stack memory and heap memory, respectively

We define two reference types

    var mother = {age:21};    var person = {age:22};

They store the identifier of the variable in the stack memory and a pointer to the object in the heap memory value (that is, the reference, the JavaScript access reference class is essentially the reference to the reference type), and the object is stored in the heap memory. We know that JavaScript does not allow us to manipulate the location of the memory directly, which means that we cannot manipulate the object's memory space directly, then we need to manipulate the object's reference instead of the object.

This pointer to the heap memory can also be understood as a reference to the object, which we access through the object's reference to the object stored in the heap memory.

2. The value of a reference type can be changed dynamically

Let's take a look at the code

                var mother = new Object ();//Create an instance of object                mother.name = ' Itnao ';//You can assign him                mother.sayname = function () {alert ( Mother.name);} You can also write a method to him                mother.sayname ();//itnao                delete mother.name;                Mother.sayname ();//undefined
From the above code we can see that we can dynamically add properties and methods to the reference type, and can delete the property at any time.

3. Comparison of size of reference types

The size comparison of reference types differs from the basic type, so let's review the comparison of the basic types first.

            var mother = "{}";            var  father = "{}";            Alert (mother = = father);//true
We can see that the result is true because mother and father are string types, and these two comparisons can be seen as comparing the actual values stored in the variable.

            var mother = {};            var father = {};            Alert (mother = = father);//false

Here we have used object literals for both mother and father, and we have set up two objects, but we can see that they are not equal because the reference type is accessed by reference, and the value that the reference type stores in the stack memory is the address of the heap memory. So the address of a reference to two objects is not the same, so the result cannot be the same.


is not the feeling Lou Zhu is very ink, not hurriedly enter the topic to tell the question of the beginning of the answer, in fact, the landlord is such a person, not angry to hit me AH!!!!!

three. Problem solving

Let's start explaining the first question.

    var a=1;    var b=a;    alert (b);//What do you think B equals?    a = 3;//What do you think I'm going to do?    alert (b);//Do you know how much B is at this time? </span>

Before I explain it, I'll show you the flowchart of this problem.

We can see that when we declare the base type a = 1, the stack memory creates a variable identifier and the value of the variable, and when we let b=a, we create an identifier B in the stack memory and copy the 1 to B, which can also say that 1 in B is a copy of 1 in a. This is equivalent to the white rich beauty of the copy, so there is no effect between the copy, you can tear the copy of your hand, but my hands have not changed, so you change the value of a will not affect the value of B. So it turns out that everyone should be very clear.

Practical Answer: If copying the base type from one variable to another is worthwhile, create a new value on the variable object and copy the value to the location assigned to the new variable.

We begin to answer the second example question

                var mother = new Object ();                Mother.name = ' Itnao ';                var son1 = mother;                var son2 = mother;                alert (son1.name);                alert (son2.name);                Mother.name = ' Tomommi ';                alert (son1.name);//guess what can be shown here                alert (son2.name);//ibid.                Delete mother.name;//guess what type can use this method                alert ( Son1.name);//guess what results to output
Let's take a look at the picture first.


If we want to ask mating to his ID card, we need to call mating, our address book will store the white rich beauty of the phone, this phone is equivalent to the pointer to mating, 100 people looking for mating, there will be 100 the same white rich beauty of the phone, this phone number is equivalent to a reference, You can only find him by calling mating, not directly, because you probably don't know him or where he is right now. So the diagram we can see, the variable mother holds a new instance of an object. This value is then copied to Son1 and Son2, in other words mother,son1 and Son2 point to the same object, so reaching, we have modified the name of mother, the other two will follow.

practical Answer: When one variable copies the value of a reference type to another variable, the value stored in the variable is also copied into the space allocated for the new variable. The difference is that this worthy copy is actually a pointer, and this pointer is pointing to an object within the heap, and after the copy operation is over, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other variable.






Simple analysis of basic types and reference types for JavaScript

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.