Moment For Technology

JS method parameter passing (pass by value)

Posted on June 24, 2022, 2:20 a.m. by Caitlin Taylor DDS
Category: The front end Tag: javascript The front end

This is the fifth day of my participation in the August Wen Challenge.More challenges in August

In one sentence

JS method arguments are passed by value

The premise

ES variables are divided into values of two data types: base type and reference type

Basic type storage

The value of the basic type is a fixed size when applying for memory, so it is stored in the stack memory. Therefore, when copying the variables of the basic type, a new memory space is created in the stack memory for storage

Reference type store

However, the value size of a reference type variable is not fixed and can be arbitrarily changed. The essence of a reference type variable is a pointer variable pointing to a certain memory area, so the reference type variable is stored in the heap memory of free memory allocation. A direct copy (reassignment) of a reference variable simply points to the same memory area (i.e. two Pointers to the same memory area)

4) Analysis of citation type

Reference value of the type of object is stored in the memory, but with JS language does not allow direct access to the memory of location (i.e., not directly manipulate the object's memory space), so the operation of the object is actually in operation object reference, rather than the direct operation of the actual object itself (i.e. the value of a reference type is by reference to access, this reference can be understood as a handle to the object)

In particular

Many languages represent strings as objects, so they are reference type variables, but ES is not

Break down

All function (method) arguments in ES are passed by value. That is, when a method is called, the value of the argument passed to the method is copied to the parameter inside the function (the argument is copied to the parameter).

The specific classification

JS can access variables either by value or by reference, but parameters are only passed by value

Passing to parametersBasic types ofThe value of the

The passed value is copied to a local variable (which is the parameter, in ES, an element of the Arguments object)

The instance
function addTen(num) {
  num += 10;
  return num;
}
var count = 20;
var result = addTen(count);
// See if the original variable is affected
alert(count);
alert(result)
Copy the code

The execution result

20
30
Copy the code

Passing to parametersReference typesThe value of the

JS is passed value address is copied to a local variable, because the copy is the address, so the function executes, function parameters within the function change will affect the function of external this reference the value of a variable, because two address pointing to the same memory region, but at the end of the function, the function of internal local variables is destroyed, will disappear

The instance
function setName(obj) {
  obj.name = 'tom';
}
var person = new Object(a); setName(person);// When person is passed to setName, obj and Person point to the same memory, so modifying the memory area that obj points to will affect Person
alert(person.name);
Copy the code

The execution result

tom
Copy the code
Analysis of the

Because the object to which Person points exists only one in heap memory, and it is a global object

Verify that parameters are passed by value rather than by reference

function setName(obj) {
  obj.name = 'tom';
  obj = new Object(a); obj.name ='jerry';
}
var person = new Object(a); setName(person);// When person is passed to setName, obj and Person point to the same memory, so modifying the memory area that obj points to will affect Person
alert(person.name);
Copy the code

The execution result

tom
Copy the code
conclusion

The object obj regenerated inside the function and its new assignment to Jerry does not change the corresponding property value of Person outside the function

If assigning to a reference type parameter is by reference, then the name of person should be jerry, because if the parameter obj takes a reference to person instead of the value of the person reference, then when a new object is generated internally and obj is redirected, the reference of the parameter obj changes. The external person reference should also be changed, but it turns out that alert(Person.name) still shows Tom, so function arguments are passed by value even if they are of reference type

When you actually override obj inside a function, that variable refers to a local object variable that is destroyed when the function finishes executing

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.