JavaScript _javascript tips on basic type values and reference type value issues raised by value assignment for input

Source: Internet
Author: User

When I was doing something, I met a problem. Take the search example on the right of the home page of the blog park, use the console operation.

  

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

It wasn't hard, so I did what I understood.

The code is as follows:

  

At this point, the value of ID zzk_q should be a test, that is, the input box should display the test. But the result.

  

Why, why has not changed, ah, and back and forth to change the way the son try, or not, of course, the basic code is still like that. Suddenly remembered that I have met such a problem before, carefully recall the solution at that time (it seems to have not understood at the time, just find a way to pass), think of it, I try, the code is as follows:

Look at the results:

 

This time it became. The first time I met this problem, I didn't think it through, but I skipped it when I succeeded. But this time I started to think why? Why, then? How can this ah, no way to understand AH. Then I went back and forth on my own, but I still couldn't figure it out. is the same assignment the difference between those two? Where's the difference? Later I knew it was a value type and a reference type, of course it was pointed out by someone else (...). )。

Then I went to find something to look at and find this thing I've seen, Khan.

Since 1997 JavaScript has been standardized, it defines six basic types. Until any one of the values in the ES6,JS program falls into one of the following categories.

undefined
NULL
Boolean
number
string
Object

However, ES6 has added a basic type: Symbol type. This is not much understanding, not to discuss, and so later familiar with it, but also learn.

In a JavaScript variable, there are two types of values: the base type and the value of the reference type. The base type value (also known as a value type) is a simple data segment that is accessed by value and operates on its values. Reference type value values are objects that can have more than one value. When assigning values, the interpreter must determine whether the value is a base type or a reference type.

The basic data types are: Undefined, Null, Boolean, number, String. A reference type is an object that is stored in memory, object, which is a combination of methods and properties.

  1. Dynamic properties of type values

  This is the reference type:

var person = new Object ();   
Person.name = "Foo";
Console.log (person.name);//foo
Delete person.name;

In this example, we first create an empty object, then save it in the person variable, and then add a property name to the object, and assign the property a string "foo", and then output, you can see the output of String foo, and then we delete this property, Output undefined. These instructions, we can dynamically add properties and methods to the object, if not destroy the object or delete the attribute, will always exist.

 This is the basic type:

var name = "Foo";
Name.age =;
Console.log (name.age);//undefined

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

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

 2. Copy variable values

As mentioned above, the base type is accessed by value. and the reference type, which is different in JavaScript and other languages, allows direct access to the location in memory, which means that we can't manipulate the object's memory space directly, so what can we do? When manipulating an object, it is actually a reference to the action object, and the value of the reference type is accessed by reference object. A reference type of storage requires both memory stack memory and heap memory to be completed, stack memory to hold the variable identifier and a pointer to the object in the heap memory, or the address of the object in the heap memory.

First look at the example:

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

Copying a value of the base type from one variable to another, we recreate a new value on the variable object and then copy the value to the location where the new variable is assigned. The two values are completely opposite, and other actions on the two variables do not affect each other. They should be stored in stack memory, as shown in the following illustration:

 

  Take a look at the reference type:

var obj1 = new Object ();
var obj2 = obj1;
Obj1.name = "Foo";
Console.log (obj2.name);  Foo
obj2.age =;
Console.log (Obj1.age);  

When you copy a value of a reference type from one variable to another, the value is also copied into a new space. But as mentioned above, the storage of reference types is done with stack memory and heap memory, which is actually a pointer to an object stored in the heap. At the end of the copy operation, the two variables are actually the same pointer, which refers to the same object. So, to change one of these variables, the other variable will change as well. The following figure:


See JavaScript advanced Programming.

Such a comb, on the beginning of the problem some understand that the beginning of the error, at first, take the value of input (this time is empty), copy to title, and then change the title to change the value of input. But the value of input (which can be regarded as a variable) is a basic type, after replication, they are completely independent, and do not affect each other. Again successful, will value out, first copy the input (object) to title, and then add the value attribute to title, and assign values, when two point to the same object, change one, also affect another. Well, that's it.

Although a lot of knowledge from the book or other places to see it over or over, but when you really meet the feeling very strange. How can this happen, and then find the answer by yourself. Wait to find or others pointed out, only to find that this has seen before, and some even solve their own (can not be said to solve, can only say no thorough, not thoroughly understand). And some of the foundation of the University have forgotten 7788 (originally did not learn well). Even the stack memory and heap memory are searched. Well, since I decided to go this way, I will study well.

At last:

Good good Coding,day day up!

PS: (Collection and reference type, basic data type assignment is not the same) a simple Java problem assignment problem

 <span style= "White-space:pre" > </span>List<person> List = new arraylist< 
Person> (); 
<span style= "White-space:pre" > </span>person pp = new person (); 
<span style= "White-space:pre" > </span>list.add (PP); 
<span style= "White-space:pre" > </span>pp.setivalue (12); 
<span style= "White-space:pre" > </span>pp.setivalue (20); 
<span style= "White-space:pre" > </span>pp = null;; 
<span style= "White-space:pre" > </span>int b = 0; 
<span style= "White-space:pre" > </span>int a = b; 
<span style= "White-space:pre" > </span>b = 8; 
<span style= "White-space:pre" > </span>system.out.println (a); <span style= "White-space:pre" > </span>for (Person ppp:list) {<span style= "White-space:pre" > </ 
Span>ppp.getivalue (); <span style= "White-space:pre" > </span>} <span style= "White-space:pre" > </span> 

The objects in the list will not change, but you can modify the property values within the object.

The value in a simple string doesn't change.

Results:

11
8888

Remember: It's best to write as normal 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.