Home Web Front-end JS Tutorial Should JavaScript function parameters be passed by value (byVal) or by address (byRef)? Share_javascript skills

Should JavaScript function parameters be passed by value (byVal) or by address (byRef)? Share_javascript skills

May 16, 2016 pm 05:30 PM
Pass value Function parameters

Regarding the question of "whether JavaScript function parameters are passed by value (byVal) or by address (byRef)", there is a common misunderstanding: "simple types" such as number and string are passed by value, Number, String, Object, Array, etc." Complex type" is pass-by-address.
Isn’t this wrong? Why is there such a misunderstanding? Take a look at these two pieces of code:

Copy the code The code is as follows:

//Cause value passing Imaginary code
function modifyLikeByVal(x){
x = 1;
console.log('x = %d', x);
}
var x = 0;
console.log('x = %d', x); // Output x = 0
modifyLikeByVal(x); // Output x = 1
console.log('x = %d', x ); // Output x = 0 x has not changed!

Copy code The code is as follows:

//Causes the false address transmission The code of
function modifyLikeByRef(x){
x[0] = 4;
x[1] = 5;
x[2] = 6;
console.log('x = [ %s ]', x.join(', '));
}
var x = [1, 2, 3];
console.log('x = [ %s ]' , x.join(', ')); // Output x = [ 1, 2, 3 ]
modifyLikeByRef(x); // Output x = [ 4, 5, 6 ]
console.log( 'x = [ %s ]', x.join(', ')); // Output x = [ 4, 5, 6 ] x has changed!

So, from the above code, we can conclude that "simple type" is passed by value (byVal) as a parameter, and "complex type" is passed by address (byRef) as a parameter.

What’s the problem?

If you carefully observe the two functions, you can find one thing:
In byVal, the parameter x is directly modified: x = 1;
And in byRef, the member of the parameter x is modified: x[0] = 4; x[1] = 5; x[2] = 6;

My guess is that in JavaScript, all variables or members are pointers. When modifying the value of a variable or member, the address of the pointer is actually modified.

So the above code can be explained:

In "byVal":

Copy code The code is as follows:

global { / / represents the global scope, and the following represents the function scope
var x = 0; // Initialize the pointer x and point to the number 0
fun(x) {
x = global.x; // Pass in Parameter global.x; The x pointer address of the fun field points to the number 0
x = 1; // Modify the x pointer address of the fun field to point to the number 1;
} // fun The domain ends, the x pointer in the global domain has not changed
}

In "byRef":
Copy code The code is as follows:

global { // represents the global scope, the following represents the function scope
/*
Initialize the pointer x and point to the array [1, 2, 3]
is actually the three members of x, 0, 1, 2, pointing to 1, 2, 3 respectively;
*/
var x = [1, 2, 3];
fun(x) {
x = global.x; // Pass in the parameter global.x; the x pointer address of the fun field points to the array [1, 2, 3]
/ + = 4;
x[1] = 5;
x[2] = 6;
} // The fun field ends, the x pointer in the global field has not changed, but its three member pointers have been changed , so we see the result we output
}


So how do you explain this code? ? ?



Copy code
The code is as follows:(function(a, b){ arguments [0] = 1;
b = 2;
console.log(arguments, a, b);
})(-1, -2);


only You can say a, b..., which is an alias for arguments[0],...[n].

If there is something wrong, please point it out, thank you.

If you have a better explanation, please share it.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between C++ function parameter passing methods and thread safety The relationship between C++ function parameter passing methods and thread safety Apr 12, 2024 pm 12:09 PM

Function parameter passing methods and thread safety: Value passing: Create a copy of the parameter without affecting the original value, which is usually thread safe. Pass by reference: Passing the address, allowing modification of the original value, usually not thread-safe. Pointer passing: Passing a pointer to an address is similar to passing by reference and is usually not thread-safe. In multi-threaded programs, reference and pointer passing should be used with caution, and measures should be taken to prevent data races.

C++ compilation error: Duplicate definition of function parameters, how to solve it? C++ compilation error: Duplicate definition of function parameters, how to solve it? Aug 22, 2023 pm 12:33 PM

As an efficient programming language, C++ is widely used in various fields because of its reliability. However, in the process of writing code, we often encounter some compilation errors, and repeated definition of function parameters is one of them. This article will detail the reasons and solutions for repeatedly defining function parameters. What is repeatedly defining function parameters? In C++ programming, function parameters refer to variables or expressions that appear in function definitions and declarations and are used to accept actual parameters passed when a function is called. When defining a function's argument list, each argument must be

Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Detailed explanation of C++ function parameters: implementation methods, advantages and disadvantages of indefinite parameter passing Apr 28, 2024 am 09:48 AM

C++ indefinite parameter passing: implemented through the... operator, which accepts any number of additional parameters. The advantages include flexibility, scalability, and simplified code. The disadvantages include performance overhead, debugging difficulties, and type safety. Common practical examples include printf() and std::cout, which use va_list to handle a variable number of parameters.

Detailed explanation of C++ function parameters: the essence and precautions of the outgoing mechanism Detailed explanation of C++ function parameters: the essence and precautions of the outgoing mechanism Apr 27, 2024 pm 12:00 PM

There are two ways to pass function parameters in C++: call by value (which does not affect the actual parameters) and call by reference (which affects the actual parameters). Passing out parameters is achieved by passing a reference or pointer, and the function can pass the value to the caller by modifying the variable pointed to by the parameter reference or pointer. Please note when using: The outgoing parameters must be clearly declared, can only correspond to one actual parameter, and cannot point to local variables within the function. When calling by passing a pointer, be careful to avoid wild pointers.

What are the PHP function parameter types? What are the PHP function parameter types? Apr 10, 2024 pm 04:21 PM

PHP function parameter types include scalar types (integers, floating point numbers, strings, Boolean values, null values), composite types (arrays, objects) and special types (callback functions, variable parameters). Functions can automatically convert parameters of different types, but they can also force specific types through type declarations to prevent accidental conversions and ensure parameter correctness.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

How to choose how to pass C++ function parameters? How to choose how to pass C++ function parameters? Apr 12, 2024 am 11:45 AM

When choosing a function parameter passing method in C++, there are four options: passing by value, passing by reference, passing by pointer, and passing by const reference. Passing by value creates a copy of the parameter value and does not affect the original parameter; passing a reference to the parameter value by reference allows the original parameter to be modified; passing a pointer to the parameter value by pointer allows modification of the original parameter value through the pointer; passing parameter value by const reference The const reference can only access the parameter value and cannot modify it.

The essence and principle of function parameter passing The essence and principle of function parameter passing Apr 12, 2024 pm 01:12 PM

Function parameter passing essentially determines how the function obtains and modifies external variables. Under pass-by-value, the function obtains a copy of the value of the incoming variable, and modifications to the copy do not affect the external variables; under pass-by-reference, the function directly receives the reference to the external variable, and modifications to the parameters also modify the external variables.

See all articles