Talk in JavaScript variables, pointers and references

Talk in JavaScript variables, pointers and references

1, variable

We may have a question: programming language variables in the end what does it mean? 
In fact, when we define a variable a, it is to specify a group of memory cells in a memory, and the set of memory cells is designated a. Actually it describes the value of variable a is the set of specific information stored in the storage unit. 
For example, in JS

There are a; 
a = 10;

The first statement in a specified memory the group of memory cells, and was named a; 
second statement number 10 is stored in the set of storage unit. 
Variable a mean value of 10 is actually a group information storage unit 10 is stored. 
If we again a copy operation:

a="hello";

Such a value into the string "hello". It is easy to understand, we will group information storage unit to a stored string "hello", the numeral 10 is clearly the original will be overwritten.

2, the pointer

If we stored in a variable address memory of what happens in another variable b in? 
It is easy to think of, direct access to the b variable, the value obtained is not a variable, but a variable address in memory, it is called a pointer variable b. 
The question arises: how to access the value of a variable by variable b it? 
In the C language, commonly used by *, such as:

int c = 10 , B;
 int * p; / * p is a pointer to an int pointer * / 
p = & c; / * & c acquisition variable c of the address, and then assigned to the variable p, so that p stored is variable c Address , i.e., p is a pointer pointing to c * / 
B = * p; / * * p p points of access to the object, then the value is assigned to B * /

In JS, this is not a pointer variable type, but the pointer is everywhere applied. such as:

var o1 = {b: 1 };
var o2 = {b: 1 }; 
o1 === o2; // false 
o1 == o2; // false

Here o1 and o2 are the same object, why not equal it? This requires in-depth understanding and pointer reference types in JavaScript. 
First, we need to understand:

O1 and o2 to assignment the address is not stored in the object o1 {b: 1}, o2 address hold objects {b: 1}.

Secondly, we need to understand the operation actually happened:

var o1={b:1}Enables the creation of an object in the memory heap {b: 1}, o1 target address is stored in the heap memory, i.e. o1 is a pointer to {b: 1}; 
Similarly, var o2={b:1}also in the heap creates an object {b: 1}, o2 stores the object in the heap memory address, i.e., o2 is a pointer to {b: 1}; 
and, due to the two identical objects {b: 1} is successively created in heap memory is not stored in the same address.

Then, we also need to know:

In JavaScript, the comparison reference type (objects, arrays, regular, Date, function) is actually relatively pointer is pointing to the same period of address in memory, only point to the same address to the same.

Obviously, the first object is a pointer to the heap memory o1 created {b: 1}; 
O2 heap memory pointer points to create a second object {b: 1}; 
but two independent objects, is not the same an object, therefore o1 and o2 does not point to the same memory address of the heap, and therefore are not equal. 
We look at common applications:

var o = {a 1 }; 
about proto__ .__ === Object.prototype; // true

O is an object constructor Object, Object attributes have a prototype and prototype is a pointer to an object of his memory, this object will be shared object instance is created by the constructor. 
As examples of Object, o has a pointer __proto__, which points to an Object object prototype property points. 
Returns true congruent here, clearly demonstrates the same point both the heap memory address, pointing to the same object. 
If we want to take the initiative to make two references point to the same type of objects, how does it work?

was obj1 = {b 1 };
was obj2 = obj1; 
obj1 === obj2; // true 
obj1 == obj2; // true

See, for reference types, the direct use of '=' fact that the assignment of both point to the same object. 
Therefore, we guess, you will see the modified object If you modify the value of the object by obj1, obj2 visit again when:

obj1.name='ls';
obj1;//{b: 1, name: "ls"}
obj2;//{b: 1, name: "ls"}

indeed so. As a comparison:

o1.name='ls';
o1;//{b: 1, name: "ls"}
o2;//{b: 1}

So, for the basic types of it?

was s1 = 1 ;
was s2 = 2 ; 
s1 === s2; // true

In JS, for primitive types simply equal value, then the two variables are equal.

3, reference

First of all, we need to understand in-depth reference value type. 
We saw earlier, obj1 and obj2 refer to the same object heap stored in memory. When we visit obj1 and obj2, it will return to the same object. It can be said: the same value as the value of obj1 and obj2 is. 
For o1 and o2, they point to the heap two {b: 1} different addresses of objects, o1 and o2 have different values.

Therefore, for reference types, we are talking about values, referring to the object stored in memory. If it is the same object, the same values, the value of different objects different.

In JS, the transfer parameters are passed by value. such as:

var a1=1,b1=2;
function add(a,b){
    a++;
    b--;
    return a+b;
};
add(a1,b1);//3
a1;//1
b1;//2

Here, the parameter function add a, b respectively variables a1, b1 value of the copy, which is passed by value. 
Does not affect the global variables a1, b1 in the add function execution environment for a, b operations. 
Look reference type:

 function setName(obj){
     obj.name="Nicholas";
     obj=new Object();
     obj.name="Greg";
 }
 var person=new Object();
 setName(person);
 alert(person.name);//"Nicholas"

Execution setName(person)time, person at the address in memory will be passed obj, obj also makes the point to the same memory address, that is, the same object. Here passed by value, the memory address is transferred. 
If you modify the object by obj, external access person will also be reflected. 
We may have a question, since it is pointing to the same object, why is not passed by reference it? 
First of all, we see the internal function obj been re-assigned so that obj points to the newly created object. If it is passed by reference, the external person will also point to the newly created object. In fact, person or point to the original object.

For reference types passed by value, in fact, be more popular understanding:

1, the argument at the memory address is transferred to the parameter, the value passed by value means a memory address; 
2, parameter modifying its objects and the common point of argument, real participants reflected outside; 
3 but never been able to modify the parameter memory address arguments directed, that is, if the parameter point to the new target, the argument does not point to the new object.

Based on the above three points, we can understand the results of the above code is run.

 

 

 

Guess you like

Origin www.cnblogs.com/zjx304/p/10986188.html