SoFunction
Updated on 2025-03-01

Examples of variables, pointers and reference functions and operations in JavaScript

This article describes the functions and operations of variables, pointers and references in JavaScript. Share it for your reference, as follows:

1. Variables

We may have a question: What do variables in programming languages ​​mean?

In fact, when we define a variable a, we specify a set of memory cells in the memory and name this set of memory cells a. The value of variable a actually describes the specific information stored in this set of memory units.

For example, in JS

var a;
a=10;

The first statement specifies a set of memory cells in memory and is named a;

The second statement stores the number 10 in this set of storage units.

The value of variable a is 10 actually means that the information stored in the storage unit group a is 10.

If we copy a again:

a="hello";

This way the value of a becomes the string "hello". This is easy to understand. We change the information stored in the storage unit group a to the string "hello", and obviously the original number 10 will be overwritten.

2. Pointer

What happens if we store the address of variable a in memory in another variable b?

It is easy to think that directly accessing variable b will not get the value of variable a, but the address of variable a in memory, and variable b is called a pointer.

Such a question arises: How to access the value of variable a through variable b?

In C language, the commonly used one is to use *, such as:

int c=10,b;
int *p;/*p is a pointer to type int*/
p=&c;/* &c gets the address of variable c and then assigns it to variable p. In this way, p stores the address of variable c, that is, p is a pointer to c*/
b=*p;/* *p accesses the object pointed to by p and assigns the value to b*/

In JS, there is no variable type like pointer, but the application of pointers is everywhere. for example:

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

Here o1 and o2 are the same objects, why aren't they equal? This requires a deep understanding of reference types and pointers in JavaScript.

First, we need to understand:

Assign values ​​to o1 and o2, not objects stored in the o1 address{b:1}, Objects are also stored in the o2 address{b:1}

Secondly, we need to understand what actually happens:

var o1={b:1}Implements the creation of an object {b:1} in the heap memory, and o1 stores the address of the object in the heap memory, that is, o1 is a pointer pointing to {b:1};

Similarly,var o2={b:1}An object {b:1} is also created in the heap memory, and o2 stores the address of the object in the heap memory, that is, o2 is also a pointer, pointing to {b:1};

Moreover, since two identical objects {b:1} are created one after another, they are not stored at the same address in heap memory.

Then, we also need to know:

In JavaScript, comparison of reference types (object, array, regular, Date, function) is actually to compare whether the pointer points to the same address in memory. Only by pointing to the same address can it be equal.

Obviously, the pointer of o1 points to the first object created in heap memory {b:1};

The o2 pointer points to the second object created in heap memory {b:1};

However, the two objects are relatively independent and are not the same object, so o1 and o2 do not point to the same heap memory address, so they are not equal.

Let's look at common applications:

var o={a:1};
o.__proto__===;//true

The constructor of object o is Object, and Object has oneprototypeproperty, and prototype is a pointer that points to an object in memory, which will be shared by an object instance created by the constructor.

As an instance of Object, o also has a pointer__proto__, it also points to the Object'sprototypeThe object pointed to by the property.

The congruent return true here clearly indicates that both point to the same heap memory address, that is, pointing to the same object.

How do we do it if we want to actively let two reference types point to the same object?

var obj1={b:1};
var obj2=obj1;
obj1===obj2;//true
obj1==obj2;//true

As you can see, for reference types, using '=' directly assigning is actually to make both point to the same object.
Therefore, we guess that if the object's value is modified through obj1, obj2 will see the modified object when accessed again:

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

That's true. As a comparison:

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

So, what about the basic types?

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

In JS, for basic types, as long as their values ​​are equal, the two variables are equal.

3. Quotation

First, we need to understand the values ​​of reference types in depth.

As we saw earlier, obj1 and obj2 point to the same object stored in heap memory. When we access obj1 and obj2, we will return the same object. It can be said that the value of obj1 and obj2 are the same.

For o1 and o2, they point to two {b:1} objects with different addresses in heap memory, and o1 and o2 have different values.

Therefore, for reference types, we refer to values, which are objects stored in memory. If it is the same object, the values ​​are the same, and different objects have different values.

In JS, the passed parameters are passed by value. for example:

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

Here, the formal parameters a and b in the function add obtain copies of the values ​​of variables a1 and b1, respectively, which is passed by value.

In the execution environment of the add function, operations on a and b will not affect the global variables a1 and b1.

Let's look at the reference type:

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

implementsetName(person)When the address in memory pointed to by person is passed into obj, so that obj also points to the same memory address, that is, the same object. Here, the value-based pass is passed, and the memory address is passed.

If the object is modified through obj, external access person can also be reflected.

We may have a question: since it is pointing to the same object, why not pass by reference?

First, we see that obj is reassigned internally, so 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, the person still points to the original object.

For reference types passing by value, it can actually be understood more commonly:

1. The actual parameter passes the pointed memory address to the formal parameter, and the value passed by the value refers to the memory address;
2. After the formal parameter modifies the object that it points to together with the actual parameter, the external actual parameter will be reflected;
3. However, the formal parameters cannot modify the memory address pointed to by the actual parameter, that is, if the formal parameters are pointed to a new object, the actual parameters will not point to the new object.

Based on the above 3 points, it is not difficult for us to understand the results of the above code running.

For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript array operation skills》、《A complete collection of operations and techniques related to JavaScript events》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript DOM skills"and"Summary of JavaScript characters and string operation techniques

I hope this article will be helpful to everyone's JavaScript programming.