This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Arguments to all functions in ECMAScript are passed by value. This means that the values outside the function are copied into the parameters inside the function, just as they are copied from one variable to another.

If it is a raw value, then it is the same as a copy of the original value variable, if it is a reference value, then it is the same as a copy of the reference value variable. For many developers, this can be difficult to understand, since variables are accessed by value and by reference, whereas passes are only passed by value.

When an argument is passed by value, the value is copied to a local variable (that is, a named parameter, or in ECMAScript terms, a slot in the argument object). When passing parameters by reference, the value’s location in memory is stored in a local variable, which means that changes to the local variable are reflected outside the function. (This is not possible in ECMAScript.)

Here’s an example:

Function addTen(num) {num += 10 return num} let count = 20 const result = addTen(count) console.log(count console.log(result) // 30Copy the code

Here, the function addTen() takes one argument, num, which is actually a local variable. When called, the variable count is passed as an argument. The value of count is 20, which is copied to the argument num for use inside addTen(). Inside the function, the value of the num argument is added by 10, but this does not affect the original variable count outside the function. The num parameter and the count variable do not interfere with each other, they just happen to hold the same value. If num is passed by reference, then count is also changed to 30. This fact is evident when using raw values such as numerical values. However, if objects are passed in variables, this is less clear.

For example, consider this example:

} let person = new Object() setName(person) console.log(person. Name) // 'Copy the code

This time, we create an object and store it in the variable person. The object is then passed to the setName() method and copied into the argument obj. Inside the function, obj and person both point to the same object. As a result, obj accesses objects by reference even if they are passed into the function by value. When a function internally sets a name attribute for obj, objects outside the function also reflect the change, because the object obj points to is stored in global scope heap memory. Many developers make the mistake of thinking that when a change is made in a local scope and the change is reflected globally, it means that parameters are passed by reference. To prove that objects are passed by value, let’s take a look at the modified example below. Example:

Function setName(obj) {object.name = new Object() object.name = 'person'} let person = new Object() setName(person) Console.log (person.name) //Copy the code

The only change in this example is the addition of two lines of code in setName(), redefining obj as a new object with a different name. When Person is passed setName(), its name property is set to what. Then the variable obj is set to a new object and the name property is set to small. If Person is passed by reference, then Person should automatically change the pointer to an object with a small name. However, when we access person.name again, its value is undefined. This indicates that after the value of the parameter in the function changes, the original reference remains unchanged. When obj is overridden inside the function, it becomes a pointer to the local object. The local object is destroyed at the end of the point to the function.

Note: In ECMAScript, function arguments are local variables

Summary of small examples:

Function setArray(arr) {arr[0] = 1; var arr = new Array(); Arr [0] = 6; return arr; } var lastArr = new Array(); lastArr[0] = 0; var newArr = setArray(lastArr); console.log(lastArr[0] + '|' + newArr[0]); / / 1 | 6 / / object function setNameAgain (obj) {obj. Name = 'aaa'; var obj = new Object(); Name = 'CCC '; // If passed by reference, obj should be rereferred to the new memory unit obj. return obj; } var person = new Object(); person.name = 'bbb'; var newPerson = setNameAgain(person); console.log(person.name + ' | ' + newPerson.name); //aaa | cccCopy the code

When passing a primitive type to an argument, the passed value is copied to a local variable (an element in the Arguments object).

When passing a value of a reference type to an argument, the memory address of that value is copied to a local variable, so changes to the local variable are reflected outside the function!

Important when need to make clear reference type address and pointer to address!!

The last

Public number: xiao He growth, The Buddha department more text, are their own once stepped on the pit or is learned

Interested partners welcome to pay attention to me, I am: He young life. Everybody progress duck together