Question about basic and reference type values caused by the value assignment of input by JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the information about the basic type value and reference type value problems caused by the value assignment of input by JavaScript. If you need a friend, you can refer to the following when you are doing something on your own, I encountered a problem. Take the search on the right of the homepage of the blog garden as an example and use the console for operations.

  

  

Now I need to pass the data from another place to the input so that the data is displayed as soon as it is refreshed.

This is not difficult, so I did it according to my understanding.

The Code is as follows:

  

In this case, the value of id zzk_q should be a test, that is, the test should be displayed in the input box. But the result is ..

  

Sorry, why didn't it change? No, I tried it again, but it still didn't work. Of course the code is basically the same. I suddenly remembered that I had encountered such a problem before, and thought about the solution at that time (it seems that I didn't understand it at the time, but I just found the solution). I tried it and the code is as follows:

Result:

 

This time. I did not think about this problem for the first time. I skipped it when I succeeded. But why? Why? How can this happen? I cannot understand it. Then I went back and forth, but I still couldn't understand it. Is there any difference between the two assigning values? What is the difference? I learned later that it was a value type and a reference type. Of course it was pointed out by someone else (......).

Then I went to look for something in this area and found it I saw it and sweated.

Since Javascript was standardized in 1997, it has defined six basic types. Until ES6, any value in the JS program belongs to one of the following types.

• Undefined
• Null
• Boolean
• Number
• String
• Object

However, ES6 adds another basic type: Symbol type. I don't know much about this, so I don't want to discuss it. I will try again later.

In JavaScript variables, there are two types of values: basic type and reference type value. The basic type value (also known as the value type) is a simple data segment. It is accessed by value and its values are operated. The value of the reference type may consist of multiple values. When assigning values, the interpreter must determine whether the value is of the basic or reference type.

Basic data types include Undefined, Null, Boolean, Number, and String. The reference type is the Object stored in the memory, that is, the Object. The Object is a combination of methods and properties.

1. dynamic attributes of Type values

  This is a reference type:

var person = new Object();   person.name = "foo";console.log(person.name);//foodelete person.name;console.log(person.name)://undefined 

In this example, an empty object is created, saved in the person variable, and an attribute name is added to the object, in addition, a string "foo" is assigned to this attribute, and the output result shows that the string foo is output. Then, we delete this attribute and output undefined. These instructions allow us to dynamically add attributes and methods to an object. If the object is not destroyed or the attribute is deleted, it will always exist.

 This is the basic type:

var name = "foo";name.age = 22;console.log(name.age);//undefined

In this example, we save a string "foo" in a name variable, add an attribute age to it, assign a value of 22 to it, and then output it, as I previously thought, this output is 22, but the actual situation is undefined.

Whether this can be understood as the value of the basic type is immutable, and the reference type can be dynamically changed.

2. Copy the variable value

As mentioned above, the basic type is accessed by value. In contrast to other languages, the reference type allows direct access to locations in the memory. That is to say, we cannot directly operate on the memory space of objects. What should we do? When operating on an object, it is actually a reference to the operation object. The value of the reference type is accessed by the reference object. The memory stack memory and heap memory are required for reference storage. The stack memory stores the variable identifier and the pointer to the object in the heap memory, it can also be said that the address of the object in the heap memory.

First look at the example:

var num1 =5;var num2 =num1;//5num1+=1; //6num2;//5 

Copy a value of the basic type from a variable to another variable. We will create a new value on the variable object, and then copy the value to the assigned position of the new variable. These two values are completely opposite. Other operations on the two variables do not affect each other. They should be stored in the stack memory, as shown in:

 

  Let's take a look at the reference type:

var obj1 = new Object();var obj2 = obj1;obj1.name = "foo";console.log(obj2.name);  //fooobj2.age = 22;console.log(obj1.age);  //22 

When you want to copy a value of the reference type from one variable to another, the value is also copied to the new space. But as mentioned above, the storage of the reference type should be completed together with the stack memory and the heap memory. This value is actually a pointer, and this pointer points to an object stored in the heap. After the copy operation, the two variables are actually the same pointer, that is, referencing the same object. Therefore, if you change one of the variables, the other variables also change. For example:


See JavaScript advanced programming.

In this case, we can understand the problem at the beginning. The error at the beginning is obtained (the value of input is blank at this time) and copied to the title, then, we want to change the value of the input by changing the title. However, the input value (which can be regarded as a variable) is a basic type. After copying, the value is completely independent and does not affect each other. If it succeeds, extract the value, copy the input (object) to the title, add the value attribute to the title, and assign the value. At this time, the two point to the same object and change one, it will also affect another one. Well, that's it.

Although I read a lot of knowledge from books or other places once or multiple times, it feels strange when you really encounter it. How can this happen, and then find the answer by yourself. After finding it or being pointed out by others, I found that this was previously seen, and some even solved it by myself (I can only say that I did not go deep into it and did not understand it completely ). Some other universities have forgotten their foundations (which is hard to learn ). I even searched the stack memory and heap memory. Well, now that you have decided to take this path, study hard.

Finally:

Good good coding,day day up!

PS: (the set is different from the reference type and basic data type value assignment) A simple java problem has been assigned a value successively.

  List
 
   list = new ArrayList
  
   ();   person pp = new person();   list.add(pp);   pp.setIvalue(12);   pp.setIvalue(20);   pp = null;;   int b = 0;   int a = b;   b = 8;   System.out.println(a);   for (person ppp : list) {    ppp.getIvalue();   }   
  
 

The added objects in the list cannot be modified, but the attribute values in the objects can be modified.

The value in a simple string cannot be changed.

Result:

11
8888

Remember: it is best to write normally to avoid confusion.

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.