


Should JavaScript function parameters be passed by value (byVal) or by address (byRef)? Share_javascript skills
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:
//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!
//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":
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":
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
})(-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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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

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.

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.

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.

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.

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.

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.
